Now here in this tutorial, I’ll explain how to use CrossPagePostBack to access previous page controls to next page using PostBackUrl property of web server control in asp.net using c# or vb.net with example demo code.
In my previous tutorials, I’d explained how to show alert message from client-side, how to send gridview selected rows in email body, how to call javascript function from code-behind and other more cracking tutorials on Asp.net, JavaScript and jQuery here.
To test this example we need to use two pages, one page is used to enter data and post to another page and second page is used to get the previous page entered data and display in label or web browser. So create one project or website then add two new pages, here I’m using “PreviousPage.aspx” to post data to another page and “NextPage.aspx” to get data and display data in label control.
Passing Controls Values To NextPage Using PostBackUrl – [.aspx]
Following is the HTML Markup for your PreviousPage.aspx page, copy and paste it to your page:
<head id=”Head1″ runat=”server”>
<title>Access PreviousPage Controls To NextPage in Asp.net</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<h4>
CrossPagePostBack: Access PreviousPage Controls To NextPage in Asp.net</h4>
<table>
<tr>
<td>First Name:</td>
<td>
<asp:TextBox ID=”txtFName” runat=”server” />
</td>
</tr>
<tr>
<td>Last Name:</td>
<td>
<asp:TextBox ID=”txtLName” runat=”server” />
</td>
</tr>
<tr>
<td>Country:</td>
<td>
<asp:DropDownList ID=”ddlCountry” runat=”server”>
<asp:ListItem Text=”India” Selected=”True” />
<asp:ListItem Text=”USA” />
<asp:ListItem Text=”UK” />
</asp:DropDownList>
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:LinkButton ID=”lbtnPostData” runat=”server” PostBackUrl=”~/NextPage.aspx”
Text=”PostData” />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
There is no change in code-behind file of PreviousPage.aspx page so leave it as default.
Now open the second page that is NextPage.aspx, add follow the following steps.
Displaying PreviousPage Controls Values On Page Load – [.aspx]
Following is the HTML Markup for your NextPage.aspx page, copy and paste it to your page:
<head id=”Head1″ runat=”server”>
<title>Display the PreviousPage Controls Values in Asp.net</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<h4>
Previous Page Values</h4>
<table>
<tr>
<td>
Your Name is “<asp:Label ID=”lblFullName” runat=”server” />” and Your country
is “<asp:Label ID=”lblCountry” runat=”server” />”
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Now add the code to get the entered data of PreviousPage.aspx in NextPage.aspx code-behind file in Page_Load event, same as shown below. Choose your language that is C# or Vb.net from two choices.
Get Previous Page Data Using C# – [.cs]
{
//It is good practice to check “PreviousPage” is not null before use
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
TextBox txtFName = (TextBox)PreviousPage.FindControl(“txtFName”);
TextBox txtLName = (TextBox)PreviousPage.FindControl(“txtLName”);
DropDownList ddlCountry = (DropDownList)PreviousPage.FindControl(“ddlCountry”);
lblFullName.Text = txtFName.Text + ” “ + txtLName.Text;
lblCountry.Text = ddlCountry.SelectedItem.Text;
}
else
{
//Redirect to “PreviousPage.aspx” to test example
Response.Redirect(“PreviousPage.aspx”);
}
}
Get Previous Page Data Using Vb.net – [.vb]
‘It is good practice to check “PreviousPage” is not null before use
If PreviousPage IsNot Nothing AndAlso PreviousPage.IsCrossPagePostBack Then
Dim txtFName As TextBox = CType(PreviousPage.FindControl(“txtFName”), TextBox)
Dim txtLName As TextBox = CType(PreviousPage.FindControl(“txtLName”), TextBox)
Dim ddlCountry As DropDownList = CType(PreviousPage.FindControl(“ddlCountry”),
DropDownList)
lblFullName.Text = txtFName.Text & ” “ & txtLName.Text
lblCountry.Text = ddlCountry.SelectedItem.Text
Else
‘Redirect to “PreviousPage.aspx” to test example
Response.Redirect(“PreviousPage.aspx”)
End If
End Sub