AspnetO

We code, that works!

  • Home
  • Asp.net
  • MVC
  • Interview Questions

CrossPagePostBack: Access PreviousPage Controls to NextPage in Asp.net

By: Mayank Modi | Folls In: Asp.net, C#, VB | Last Updated: Nov 02, 2019

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.

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.

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:

<html xmlns="http://www.w3.org/1999/xhtml">
<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>&nbsp;</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.

Note: Here in this page, I used PostBackUrl=”~/NextPage.aspx” property of asp.net button control to post data to next page. It is mandatory to get that data to next page.

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:

<html xmlns="http://www.w3.org/1999/xhtml">
<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]

protected void Page_Load(object sender, EventArgs e)
{
       //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]

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       '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

Example Result

CrossPagePostBack: Access PreviousPage Controls To NextPage in Asp.net

Download Example

Icon

CrossPagePostBack: Access PreviousPage Controls To NextPage in Asp.net

1 file(s)   31.21 KB
Download This Example

Git Repo

git clone https://github.com/immayankmodi/cross-page-post-back-asp-net-example.git

Signup Today And Get Latest Tutorials For Free!

Subscribe to us and get free latest tutorials notifications whenever we publish a new contents.

About Mayank Modi

Mayank is a web developer and designer who specializes in back-end as well as front-end development. He's a Founder & Chief Editor of AspnetO. If you'd like to connect with him, follow him on Twitter as @immayankmodi.

Share Your Comments & Feedback Cancel reply

Note: To post any code in comment, use <pre>your code</pre>

Social Connections

  • 0 Fans
  • 3,222 Followers
  • 21 Followers
  • 52 Followers
  • 1,559 Subscribers

Top Posts

  • CSS3 Transition: fadeIn and fadeOut like Effects to Hide Show Elements
  • Top 10 OOPS Concepts In C# .NET With Examples
  • Parser Error While Deploying Website to Server in Asp.net
  • Asp.net TextBox: How to Get Set TextBox Value or Text in jQuery
  • How to Print Asp.net GridView Data on Button Click using Javascript?

Find by Tags

Ado.net Ajax appSettings Asp.net C# CheckBox CheckBoxList ConnectionStrings Control CSS CSS3 Difference Download DropDownList Export Facebook fadeIn fadeOut fadeTo fadeToggle File File Extension FileUpload Function GridView IIS Interview Questions JavaScript jQuery MVC OOP RadioButtonList RDP Repeater Send Mail Solutions Split SQL Stored Procedure TextBox Upload Validation VB Web.config Web Hosting

The Man Behind AspnetO

Mayank Modi

Hi there,

Myself Mayank Modi, a Full Stack Developer (.NET Stack) and a blogger from Surat, India.

I'm welcoming you to my blog - AspnetO, a programmers community blog where we code, that works!

I started AspnetO as a hobby and now we're growing day by day. We're now having 5000+ programmers that get benefits and learn new things about website design and development under our community blog.

Here at AspnetO, I write about Beginners to Advance level of tutorials on programming languages like Asp.net using C# and Vb.net, MVC, SQL Server, JavaScript, jQuery etc. In sort, all about .NET Framework and website development stuff and sometimes sharing tips and tricks that can help you to grow up your programming skills.

You can get more details about me and my blog at About us page.

Subscribe To Newsletter

Enter your email address to subscribe to this blog and receive notifications of new posts right to your inbox

Join 1000+ other subscribers

Hot on AspnetO

Icon
Gridview Insert Update Delete Example in Asp.net, C#, Vb.net 7245 downloads 39.76 KB
Download This Example
Icon
Gridview Insert Update Delete Example in Asp.net, C#, Vb.net 6211 downloads 39.76 KB
Download This Example
Icon
Gridview Insert Update Delete Example in Asp.net, C#, Vb.net 6207 downloads 39.76 KB
Download This Example
Icon
Export gridview all rows or selected rows to word, excel, text, pdf examples 3056 downloads 1.01 MB
Download This Example
Icon
Export gridview all rows or selected rows to word, excel, text, pdf examples 2993 downloads 1.01 MB
Download This Example

Copyright © 2014 - 2019 · All Rights Reserved.replica cartier watches

About | Copyrights | Privacy | Terms | Contact | Advertise | Sitemap
Previous Gridview Keep Checked or Selected Rows State During Paging in Asp.net
Next Send GridView Selected Rows in Mail Body in Asp.net