Using HTTP Post and Public Property in ASP.Net

This article decribes some of the techniques to pass values from one page to another page in ASP.Net. When you develop any website in ASP.Net containing many pages, you need to provide navigation to move or browse between pages in the website. At the time of redirection, you need some data or values to be passed from the source page to the destination page to do some processing in the destination page. To do that, ASP.Net has some techniques that can assist us to pass values from one page to another page.  

Using HTTP Post

Using this technique, I will call a "PostBackUrl" and on the next page use "Request.From".

PostBackUrl: The PostBackUrl of the page is used to post from the current page when the Button control is clicked.

Now drag and drop one TextBox and Button control onto the form.

HTTPPostSource Page

HTTPPostSource.aspx

<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox>

 <br/>

 <br/>

<asp:ButtonID="Button1"runat="server"Text="Button"OnClick="Button1_Click"/>
 

HTTPPostSource.aspx.cs

Protectedvoid Button1_Click(object sender, EventArgs e)

{

Button1.PostBackUrl ="~/HTTPPostTarget.aspx";

}

HTTPPostTarget Page

HTTPPostTarget.aspx

<%@Page Language="C#"AutoEventWireup="true"CodeFile="HTTPPostTarget.aspx.cs"

   Inherits="HTTPPostTarget"%>

 

<!DOCTYPEhtml PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headid="Head1"runat="server">

   <title></title>

</head>

<body>

   <formid="form1"runat="server">

   <div>

       <asp:LabelID="Label1"runat="server"Text="Label"></asp:Label>

   </div>

   </form>

</body>

</html>

HTTPPostTarget.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

publicpartial classHTTPPostTarget : System.Web.UI.Page

{

   protected void Page_Load(object sender,EventArgs e)

    {

        Label1.Text ="Posted Value: " + Request.Form["Textbox1"].ToString();

    }

}

Now run the application and test it.

Using Public Property

When we use a Public Property we create a a public property and on the next page get it using "PreviousPage.PropertyName". If you have 2 simple pages then one is a PublicPropertySource and the other is a PublicPropertyTarget. I have done it this way.

PublicPropertySource page: This page contains a property.

PublicPropertySource.aspx

<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox>

&nbsp;<asp:ButtonID="Button1"runat="server"
OnClick
="Button1_Click"Text="Button"/> 

PublicPropertySource.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

publicpartial classPublicProperty : System.Web.UI.Page

{

   public string TextValue

    {

       get

        {

           return TextBox1.Text;

        }

    }

   protected void Page_Load(object sender,EventArgs e)

    {

    }

 

   protected void Button1_Click(object sender,EventArgs e)

    {

        Server.Transfer("PublicPropertyTarget.aspx");

    } 

} 

PublicPropertyTarget Page

On PublicPropertyTarget.aspx add this on top.

<%@ PreviousPageType VirtualPath="Page1.aspx" %>PublicPropertyTarget.aspx

<%@Page Language="C#"AutoEventWireup="true"CodeFile="PublicPropertyTarget.aspx.cs"Inherits="PublicPropertyTarget" %>

<%@PreviousPageType VirtualPath="~/PublicPropertySource.aspx"%>

<!DOCTYPEhtml PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

   <title></title>

</head>

<body>

   <formid="form1"runat="server">

   <div>   

       <asp:LabelID="Label1"runat="server"Text="Label"></asp:Label>   

   </div>  

   </form>

</body>

</html>

PublicPropertyTarget.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

publicpartial classPublicPropertyTarget : System.Web.UI.Page

{

   protected void Page_Load(object sender,EventArgs e)

    {

        Label1.Text = PreviousPage.TextValue;      

    }

}

Now run the application and test it.

Up Next
    Ebook Download
    View all
    Learn
    View all