Use of PostBackUrl in ASP.NET 2.0 using VB.NET

In this article you can understand how the aspx page is PostBack to another aspx Page. ASP.NET 2.0 inludes the button's property to perform this task very simple.
  • 3257

Introduction:

 

In ASP.NET 1.1 a page can only PostBack to itself, it is a common technique to post the page to another page. In ASP.NET 2.0 ASPX page can post to different ASPX page by using the button's PostBackUrl property. Where ASPX is the extension of ASP.NET web application.

 

We can see the button's property in the following figure.

 

 

  1.jpg

 

Figure 1: Button's PostBackUrl property.

 

Steps for implementing the cross page PostBack using controls in ASP.NET 2.0:

 

Step 1: Open the new website in Microsoft Visual studio 2005.

 

Step 2: Drop a button control on the form and you can also drag other controls according the need of application.

 

Step 3: Set the PostBackUrl property of the button where you want to PostBack.

  

For Example: Let us suppose we want to PostBack to another page from "My Page" then we have to pass the PostBackUrl as follows:

 

PostBackUrl="http://www.c-sharpcorner.com".

 

Now you can see the source code and also set the PostBackUrl of that page where you want to post the page.

 

MyPage.aspx:

 

<%@ Page Language="VB" AutoEventWireup="true"  CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!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>My Page</title>

</head>

<body>

  <form id="form1" runat="server">

    <div>

      <table cellpadding="0" cellspacing="0" border="2" height="50px" style="width: 33%">

        <tr>

          <td align="center" valign="top" style="padding-top:20px;">

            <asp:TextBox ID="TxtName" runat="server"></asp:TextBox><br/><br/>

            <asp:Button ID="BtnSubmit" runat="server" PostBackUrl="http://www.c-sharpcorner.com" Text="Submit" OnClick="Button1_Click" />

          </td>

        </tr>

      </table>

    </div>

  </form>

</body>

</html>

 

Design view of the above source code:

2.jpg

Figure 2: My first page.

 

When you post to another page in ASP.NET 2.0, you can reach out to the values in the page using the following methods:

 

MyPage.aspx.vb:

 

 

 

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

 

Partial Public Class _Default
    Inherits System.Web.UI.Page
     Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        If Not PreviousPage Is Nothing Then
            TextBox(txtName = (TextBox))
             PreviousPage.FindControl("txtName")
             Me.TxtName.Text = txtName.Text
        End If
    End Sub
 End Class

 

Output:  After debug the .vb file you will get the output as:

 

 

  3.jpg

 

Figure 3: My Page is display.

 

After that click on Submit button "My Page" will PostBack to another page. Here we have set the PostBackUrl="http://www.c-sharpcorner.com", so the following page will open that mean the page will post to c-sharpcorner.com.

 

 

  4.jpg

 

Figure 4: New page is open.

 

PreviousPage property holds the posted page. We check if it is null and use the FindControl() method to reach our control.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.