Friday, June 15, 2012

Passing data between asp.net pages

There are so many ways in which you can pass data between asp.net web pages. Data can be passed from one page to other using Querystring, HTTP Post, Public properties, Sessionstate and Controlinfo. Below all approaches were given with an example.

1.Querystring:

Default.aspx
Data : <asp:TextBox ID="DataTextBox" runat="server" Text="Hello World!"></asp:TextBox><br />
<asp:Button ID="btnSend" runat="server" Text="Send" onclick=" btnSend_Click"/>

Default.aspx.cs
protected void  btnSend_Click(object sender, EventArgs e)
{
       Response.Redirect("QueryStringPage.aspx?Data="+Server.UrlEncode(DataTextBox.Text));
 }

TargetPage.aspx
The data received is: <%=Server.UrlDecode(Request.QueryString["Data"]) %>

2. HttpPost:

Default.aspx

Data : <asp:TextBox ID="DataTextBox" runat="server" Text="Hello World!"></asp:TextBox><br />
<asp:Button ID="btnHttpPost" runat="server" Text="Use HttpPost" PostBackUrl="~/TargetPage.aspx"/>

TargetPage.aspx

The data received is: <%=Request.Form["DataTextBox"] %><br />
<fieldset>
        <legend>Data from the source page</legend>
        <asp:Label ID="lblData" runat="server" Text="Label"></asp:Label>
</fieldset>

TargetPage.aspx.cs

using System.Text;

protected void Page_Load(object sender, EventArgs e)
{
            StringBuilder displayValues = new StringBuilder();
            System.Collections.Specialized.NameValueCollection postedValues = Request.Form;
            String nextKey;
            for (int i = 0; i < postedValues.AllKeys.Length; i++)
            {
                nextKey = postedValues.AllKeys[i];
                if (nextKey.Substring(0, 2) != "__")
                {
                    displayValues.Append("<br>");
                    displayValues.Append(nextKey);
                    displayValues.Append(" = ");
                    displayValues.Append(postedValues[i]);
                }
            }
            lblData.Text = displayValues.ToString();
}

3. Public properties

Default.aspx.cs

public string DataToSend
{
       get
       {
             return DataTextBox.Text;
        }
}


protected void btnSend_Click(object sender, EventArgs e)
{
       Server.Transfer("TargetPage.aspx");
}

TargetPage.aspx

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


The data received is: <%=PreviousPage.DataToSend %>

4. Sessionstate:

Default.aspx.cs

protected void btnSend_Click(object sender, EventArgs e)
{
       Session["Data"] = DataTextBox.Text;
       Response.Redirect("TargetPage.aspx");
}

TargetPage.aspx

The data received is: <%=Session["Data"] %>

5. Control Info
Default.aspx.cs

protected void btnSend_Click(object sender, EventArgs e)
{
       Server.Transfer("TargetPage.aspx");
}

TargetPage.aspx

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


The data received is: <asp:Label ID="lblData" runat="server" Text="Label"></asp:Label>

TargetPage.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
        var textbox = PreviousPage.FindControl("DataTextbox") as TextBox;
        if (textbox != null)
        {
             lblData.Text = textbox.Text;
         }
}

No comments:

Codeigniter Shield Authorization

Codeigniter Shield Authorization CodeIgniter Shield is the official authentication and authorization framework for CodeIgniter 4. It provide...