Tuesday, January 15, 2008

Performing Cross-Page Posts (2) Typed

As an alternative to using the FindControl() method to retrieve a particular control from the previous page, you can expose the control through a page property. The page ButtonSearchTyped.aspx exposes the txtSearch TextBox through a property named SearchString. The page posts the form data to a page named ButtonSearchResultsTyped.aspx.

ButtonSearchTyped.aspx

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Public ReadOnly Property SearchString() As String

Get

Return txtSearch.Text

End Get

End Property

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

<title>Button Search Typed</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:Label

id="lblSearch"

Text="Search:"

Runat="server" />

<asp:TextBox

id="txtSearch"

Runat="server" />

<asp:Button

id="btnSearch"

Text="Go!"

PostBackUrl="ButtonSearchResultsTyped.aspx"

Runat="server" />

</div>

</form>

</body>

</html>


ButtonSearchResultsTyped.aspx

<%@ Page Language="VB" %>

<%@ PreviousPageType VirtualPath="~/ButtonSearchTyped.aspx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Sub Page_Load()

If Not IsNothing(Page.PreviousPage) Then

lblSearch.Text = String.Format("Search For: {0}", PreviousPage.SearchString)

End If

End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

<title>Button Search Results Typed</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:Label

id="lblSearch"

Runat="server" />

</div>

</form>

</body>

</html>


Notice that the page ButtonSearchResultsTyped.aspx includes a PreviousPageType <%@ PreviousPageType %>directive. This directive casts the value returned by the PreviousPage property as an instance of the ButtonSearchTyped class. Without this directive, the PreviousPage property would return the previous page as an instance of the generic Page class.

ASP.NET 2.0 Unleashed

blog comments powered by Disqus