Post Back and Cross page posting Data in ASP.net (C#)


If you want to transfer data (Previous page) to next page. Here are the methods you can use:

Cross page postback:

This is commonly used method to post data from one page to another using postback url:

.aspx page code:

<div>
    Name : <asp:TextBox ID="txtName" runat="server">asp:TextBox><br />
    Address : <asp:TextBox ID="txtAddress" runat="server">asp:TextBox><br />
    <asp:Button ID="btnClick" runat="server" Text="Post Data"
            onclick="btnClick_Click" />
   
    div>

.aspx.cs page:

protected void btnClick_Click(object sender, EventArgs e)
    {

        Server.Transfer("PartialClassEx.aspx", true);
    }

    public TextBox Name
    {
        get
        {

            return txtName;
        }
    }

    public TextBox Address
    {
       get
        {

            return txtAddress;
        }
    }






Get Data page:

.aspx page:

Write below code in your .aspx page just below to page directive :

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

.aspx.cs page:

     if (!IsPostBack)
        {
            //post method
            string Name = Request["txtName"].ToString();
            string Address = Request["txtAddress"].ToString();
           
            //using cross page postback
           // string Name = PreviousPage.Name.Text;
           // string Address = PreviousPage.Address.Text;

            //display
            Response.Write("Here is the data " + Name + " " + Address);
        }
    }

Comments