Cross Page PostBack in ASP.NET

ASP.Net provides a feature known as Cross Page PostBack for a web form to post-back to a different web form.
  • 2424

 

ASP.Net provides a feature known as Cross Page PostBack for a web form to post-back to a different web form. This is usually required when you are creating a multi page form to collect information from the user on each page.

There are several methods to post data from one page to another page depending on your needs. When moving from the source to the target page, the values of controls in the source page can be accessed in the target page. Lets clear it more through an example:

ASP.NET Cross-Page Posting

Create a new ASP.NET website with a single webpage by default, To use cross page postback we need two web pages. So add another dafault aspx page to the website. The website will now contain two pages, Default.aspx and Default2.aspx

Design View Default.aspx

crosspageposting 1.gif

 

Here we add a textbox to enter user name, a calendar control, two submit buttons and drop a label to display the user name and selected date. In the target page Default2.aspx, also drop a label on the page from the toolbox.

Code behind Default.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">    
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
       Label1.Text = "Hello " & TextBox1.Text & "! " & "How are you?" & "<br />" & "The date you selected is: " & Calendar1.SelectedDate.ToShortDateString()
    End Sub
</script>
<
html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
  <title>ASP.NET Second Page</title>
</head>
<
body>
    <form id="form1" runat="server" style="font-family: Verdana; font-size: small">
      Enter your name:<br />
         <asp:Textbox ID="TextBox1" Runat="server" Font-Bold="True" ForeColor="#996633"/>
            <br /><br />
            <b>Which date do you want to select?<br />
            </b><br />
    <asp:Calendar ID="Calendar1" Runat="server" BackColor="#FFFFCC" 
             BorderColor="#FFCC66" BorderWidth="1px" DayNameFormat="Shortest" 
             Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399" Height="200px" 
             ShowGridLines="True" Width="220px">
                <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
                <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
                <OtherMonthDayStyle ForeColor="#CC9966" />
                <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
                <SelectorStyle BackColor="#FFCC66" />
                <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" 
                   ForeColor="#FFFFCC" />
                <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
     </asp:Calendar>
       <br />
     <asp:Button ID="Button1" Runat="server" Text="Submit page to itself"                               OnClick="Button1_Click" Font-Names="Verdana" />
     <asp:Button ID="Button2" Runat="server" Text="Submit page to NextPage"                                  PostBackUrl="NextPage.aspx" Font-Names="Verdana" />
        <br /><br />
     <asp:Label ID="Label1" Runat="server" Font-Names="Verdana"></asp:Label>
  </form>
</body>
</
html>

Code behind Default2.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">    
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
           Dim myTextbox1 As TextBox
           Dim myCalendar1 As Calendar
           myTextbox1 = CType(PreviousPage.FindControl("Textbox1"), TextBox)
           myCalendar1 = CType(PreviousPage.FindControl("Calendar1"), Calendar)
           Label1.Text = "Hello " & TextBox1.Text & "! " & "How are you?" & "<br />" & "The date you selected is: " & Calendar1.SelectedDate.ToShortDateString()
      End Sub
</script>
<
html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ASP.NET First Page</title>
</head>
<
body>
    <form id="form1" runat="server">
      <div>
      
    <asp:Label ID="Label1" Runat="server"></asp:Label>
       </div>
    </form>
</body>
</
html>

Output Window
crosspageposting2.gif

After enter name and selecting date we click on the first submit button which display the message at the bottom of the page as shown above. Now check  the page is being accessed as Cross Page postback or not by clicking on the second button.

Cross-Page Output
crosspageposting3.gif

You see the message is shown on the next web page by setting the 'PostBackUrl' property of a Button to the URL of the target page, Default2.aspx.

I hope you will like this article.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.