Using HTTP Post and Public Property in ASP.Net

This article describes 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

 

  1. <asp:TextBoxID="TextBox1"runat="server"></asp:TextBox>  
  2.  <br/>  
  3.  <br/>  
  4. <asp:ButtonID="Button1"runat="server"Text="Button"OnClick="Button1_Click"/>  

HTTPPostSource.aspx.cs

  1. Protectedvoid Button1_Click(object sender, EventArgs e)  
  2. {  
  3.     Button1.PostBackUrl ="~/HTTPPostTarget.aspx";  
  4. }  

 

HTTPPostTarget Page

HTTPPostTarget.aspx

 

  1. <%@Page Language="C#" AutoEventWireup="true" CodeFile="HTTPPostTarget.aspx.cs" Inherits="HTTPPostTarget"%>  
  2. <!DOCTYPEhtml PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <htmlxmlns="http://www.w3.org/1999/xhtml">  
  4. <headid="Head1"runat="server">  
  5.    <title></title>  
  6. </head>  
  7. <body>  
  8.    <formid="form1"runat="server">  
  9.    <div>  
  10.        <asp:LabelID="Label1"runat="server"Text="Label"></asp:Label>  
  11.    </div>  
  12.    </form>  
  13. </body>  
  14. </html>  

 

HTTPPostTarget.aspx.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. publicpartial classHTTPPostTarget : System.Web.UI.Page  
  8. {  
  9.    protected void Page_Load(object sender,EventArgs e)  
  10.     {  
  11.         Label1.Text ="Posted Value: " + Request.Form["Textbox1"].ToString();  
  12.     }  
  13. }   

Now run the application and test it.

Using Public Property

When we use a Public Property we create 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

 

  1. <asp:TextBoxID="TextBox1"runat="server"></asp:TextBox>  
  2.  <asp:ButtonID="Button1"runat="server"  
  3. OnClick="Button1_Click"Text="Button"/>   

PublicPropertySource.aspx.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. publicpartial classPublicProperty : System.Web.UI.Page  
  8. {  
  9.     public string TextValue  
  10.     {  
  11.        get  
  12.         {  
  13.            return TextBox1.Text;  
  14.         }  
  15.     }  
  16.     protected void Page_Load(object sender,EventArgs e)  
  17.     {  
  18.     }  
  19.     protected void Button1_Click(object sender,EventArgs e)  
  20.     {  
  21.         Server.Transfer("PublicPropertyTarget.aspx");  
  22.     }   
  23. } 

PublicPropertyTarget Page

On PublicPropertyTarget.aspx add this on top.

  1. <%@ PreviousPageType VirtualPath="Page1.aspx" %>PublicPropertyTarget.aspx  
  2. <%@Page Language="C#" AutoEventWireup="true" CodeFile="PublicPropertyTarget.aspx.cs" Inherits="PublicPropertyTarget" %>  
  3. <%@PreviousPageType VirtualPath="~/PublicPropertySource.aspx"%>  
  4. <!DOCTYPEhtml PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.    <title></title>  
  8. </head>  
  9. <body>  
  10.    <form id="form1" runat="server">  
  11.    <div>  
  12.        <asp:LabelID="Label1" runat="server" Text="Label"></asp:Label>     
  13.    </div>    
  14.    </form>  
  15. </body>  
  16. </html>   
PublicPropertyTarget.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. public partial class PublicPropertyTarget : System.Web.UI.Page  
  8. {  
  9.     protected void Page_Load(object sender,EventArgs e)  
  10.     {  
  11.         Label1.Text = PreviousPage.TextValue;  
  12.     }  
  13. }  

Now run the application and test it.


Similar Articles