·
Using FindControl Method
There are couple of ways of getting control values of the source
page into the target page.
Listing 1
<form id="form1" runat="server">
<div>
<asp:Label ID="lblUName" runat="server" Text="Name"></asp:Label>
<asp:TextBox ID="txtUName" runat="server"></asp:TextBox>
<br />
<asp:Label ID="lblPWD" runat="server" Text="Password"></asp:Label>
<asp:TextBox ID="txtPWD" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnGo" runat="server" OnClick="btnGo_Click" Text="GO"
PostBackUrl="crosspage2.aspx" />
</div>
</form>
The above code is for crosspage1.aspx where PostBackUrl
property is set for a button named btnGo. This property takes a string value
which points to the location of the file to which this page should post. In
this case, it is page crosspage2.aspx. This means that the crosspage2.aspx now
receives the postback and all the values contained in crosspage1.aspx page
controls. In Listing 3 the TextBox control is created and populated using PreviousPage.FindControl
property. The value of the controls in crosspage1.aspx has been displayed. Another
way of exposing the control values from the crosspage1 to crosspage2 is to create
a property of control/controls.
·
Using Control Property
Listing 2
public string Name
{
get
{
return txtUName.Text;
}
}
public string Password
{
get
{
return txtPWD.Text;
}
}
In order to work with the properties described in crosspage1,
PreviousPageType directive is required in the posting page that is in
crosspage2.aspx. (see code specified in Listing 4).
Listing 3
Response.Write("Name:" + ((TextBox)(PreviousPage.FindControl("txtUName"))).Text
+ "<BR>");
Response.Write("Password:" +((TextBox)(PreviousPage.FindControl("txtPWD"))).Text
+ "<BR>");
Response.Write("Name:" + PreviousPage.Name + "<BR>");
Response.Write("Password:" + PreviousPage.Password);
Listing 4
<%@ PreviousPageType VirtualPath="crosspage1.aspx"%>
The above directive points to crosspage1.aspx by specifying
the VirtualPath attribute and when that is in place, one can see the properties
exposed by crosspage1 in crosspage2 using the intellisence of PreviousPage
property. Note that it is recommended to expose only the information we need
as public properties to reduce the amount of information available to
potentially malicious users.
IsPostBack and IsCrossPagePostBack
A postback of a page means posting back to the same page. We
can differentiate between the page’s first request and any postbacks by using
the Page.IsPostback property which is specified in Listing 5.
Listing 5
if (Page.IsPostBack == true)
{
//Do some processing.
}
Instead of checking against a true or false value, we can
also find out that the request is not a postback as the code specified in
Listing 6.
Listing 6
if (!Page.IsPostBack)
{
//Do some processing.
}
IsCrossPostBack property, which is new in ASP.NET 2.0,
enables us to check whether the request is from a particular page or not. If
any end user request an application page (which can be accessed by registered
user only) without login, we can take advantage of IsCrossPostBack property. Using
IsCrossPostBack property, we can check whether the request is coming from a
particular page (say crosspage1.aspx page) or not and act accordingly. If we
can proceed that means we can display the requesting page or return to a
particular page (say crosspage1.aspx page). (See the code as specified in
Listing 7.)
Listing 7
if(PreviousPage!=null && PreviousPage.IsCrossPagePostBack && PreviousPage.IsValid)
{
Response.Write("Name:" +
((TextBox)(PreviousPage.FindControl("txtUName"))).Text + "<BR>");
Response.Write("Password:" +
((TextBox)(PreviousPage.FindControl("txtPWD"))).Text + "<BR>");
Response.Write("Name:" + PreviousPage.Name + "<BR>");
Response.Write("Password:" + PreviousPage.Password);
}
else
{
Response.Redirect("crosspage1.aspx");
}