Monday, 27 February 2012

.Net Beginners : How to post a page to another page in ASP.NET?

In we can also post data from one page to another page.
We can achive this through "PostBackUrl" Property of asp:Button and asp:LinkButton.
In "PostBackUrl" Property you can set the path of second page name on which you want to get posted data.

Syntax :

Example :
In First page you have like this.
 <asp:Label runat="server" ID="lblProductName" Text="Product Name :"      AssociatedControlID="txtProductName"></asp:Label>
<asp:TextBox runat="server" ID="txtProductName"></asp:TextBox>
<asp:Button runat="server" ID="btnPost" Text="Post" PostBackUrl="~/TempDefault.aspx" />
Now if we want to post Product Name on another page.You need to create public property which will return txtProductName text

The property that you need to implement in First page :
 Public ReadOnly Property GetProductName() As String
            Return txtProductName.Text
        End Get

    End Property
Now on second page you need to Register Previouse Page Type tag like this.
<%@ PreviousPageType VirtualPath="~/TestPage.aspx"%>
Here in VirtualPath property you need to set First page path from where the data will come.

Now in second page you can access First page properties like this.
Syntax :
PreviousPage.[public property]
In our example like this.

You can also create as many properties as you want for get different data.

This article is very useful for .Net Beginning.


No comments:

Post a Comment