Cross Page Posting in ASP.NET using VB.NET

In this article we demonstrate on what is Cross page Posting and how you can do Cross Page Posting.
  • 3253
 

ASP.NET by default Submits the Form to the same page. Cross Page Posting is submitting the form on different page. this is actually needed when you are creating a multipage form to collect information of the user on each page. When moving from the source to the Target page, the values of control in the source page can be accessed in the target page. Cross Page Posting feature allows a page to Cross Post to another page which in turn allows you to access all data in Posted Source Page.
To use Cross Page Posting you have to use the PostBackURL attribute to specify the Page you want to post to. Here I am discussing an example in which I am describing the Cross Page Posting or PostBack in ASP.NET using VB.NET. I have taken two TextBoxes and one Button control, Button click will Post Back to another page and their you will retrieve and show values of both TextBoxes.


Steps for implementation:-

  • Simply open a ASP.NET web application. 
  • Add two TextBoxes, one Button on Default.aspx or write code to add controls.

    <%@ Page Title="Home Page" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false"
        CodeBehind="Default.aspx.vb" Inherits="WebApplication7._Default" %>
     
    <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    </asp:Content>
    <
    asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
        <
    h1>       
            First Name: <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
        </h1>
            <h1>
            Last Name: <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
            </h1>
            <p>
            &nbsp;</p>
            <asp:Button ID="Button1" runat="server" Text="Button"
            PostBackUrl="~/Default2.aspx" />
    </
    asp:Content
    >
     

  • The design mode of Default.aspx will look like below.

    Cross-Page-Posting1.gif
     
  • Right-click on design page select view code and write below code.

        
    Public ReadOnly Property pbTxtFirstName() As TextBox
            Get
                Return txtFirstName
            End Get
        End Property
        Public ReadOnly Property pbTxtLastName() As TextBox
            Get
                Return txtLastName
            End Get
        End
    Property
     
  • Right-click on Project node and add select New Item. 
  • Select new Web Form and named it to Default2.aspx. 
  • Write the below code on Default2.aspx.

    <%@ PreviousPageType VirtualPath="~/Default.aspx" %>
    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default2.aspx.vb" Inherits="WebApplication7.Default2" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <
    body>
        <form id="form1" runat="server">
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </form>
        </body>
    </html>
     
  • Right-click on design page of Default2.aspx and select view code and write below code.

        
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If PreviousPage IsNot Nothing AndAlso PreviousPage.IsCrossPagePostBack Then
                Label1.Text = ("Welcome " & PreviousPage.pbTxtFirstName.Text & " ") + PreviousPage.pbTxtLastName.Text
            Else
                Response.Redirect("Default.aspx")
            End If
        End Sub

Output:-

Cross-Page-Posting2.gif

Cross-Page-Posting3.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.