Sunday, May 19, 2013

How to pass variable/data from one page to another in Asp.net


Introduction: In this post I try to explain how we can pass value from one page and retrieve on another page.
Description:

Here you see the various methods to pass value from one page to another using Cookies, Querystring, Session variable and Application variable.  Add two webform to application. Here one webform name is example.aspx and another name is Next.aspx.

Querystring Example:
After that on example.aspx page add the textbox and button control. As below mention:
<tabe>
       <tr><td>Book Name:</td><td><asp:TextBox ID="txtbookname" runat="server"></asp:TextBox></td></tr>
       <tr><td> Price:</td><td><asp:TextBox ID="txtprice" runat="server"></asp:TextBox></td></tr>
       <tr><td>&nbsp;&nbsp;</td><td> <asp:Button ID="btnquery" runat="server" onclick="Button1_Click" Text="Button" /></td></tr>
       </tabe>

On button click
protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Next.aspx?Book=" + txtbookname.Text + " &Price=" + txtprice.Text);
    }

In VB
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnquery.Click
        Response.Redirect(("Next.aspx?Book=" + txtbookname.Text & " &Price=") + txtprice.Text)

    End Sub

Now go to Next.aspx page and on page load the below mention code:
protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["Book"] != null)
        {
            lblbook.Text = Request.QueryString["Book"];
            lblprice.Text = Request.QueryString["Price"];
        }
    }

In VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Request.QueryString("Book") IsNot Nothing Then
            lblbook.Text = Request.QueryString("Book")
            lblprice.Text = Request.QueryString("Price")
        End If
    End Sub

Cookie Example:
<tabe>
       <tr><td>Book Name:</td><td><asp:TextBox ID="txtbookname" runat="server"></asp:TextBox></td></tr>
       <tr><td> Price:</td><td><asp:TextBox ID="txtprice" runat="server"></asp:TextBox></td></tr>
       <tr><td>&nbsp;&nbsp;</td><td> <asp:Button ID="btnquery" runat="server" onclick="Button1_Click" Text="Button" /></td></tr>
       </tabe>

On button click.
protected void Button1_Click(object sender, EventArgs e)
    {
        HttpCookie ck = new HttpCookie("book");
        ck.Values.Add("bookname",txtbookname.Text);
        ck.Values.Add("price",txtprice.Text);
        Response.Cookies.Add(ck);
        Response.Redirect("Next.aspx");
    }

In VB
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnquery.Click
        Dim ck As New HttpCookie("book")
        ck.Values.Add("bookname", txtbookname.Text)
        ck.Values.Add("price", txtprice.Text)
        Response.Cookies.Add(ck)
        Response.Redirect("Next.aspx")

    End Sub

Now on Next.aspx page load add the below mention code:
protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie cv = Request.Cookies["book"];
        if(cv != null)
        {

            lblbook.Text = cv.Values["bookname"].ToString();
            lblprice.Text = cv.Values["price"].ToString();
        }
    }

In VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim cv As HttpCookie = Request.Cookies("book")
        If cv IsNot Nothing Then

            lblbook.Text = cv.Values("bookname").ToString()
            lblprice.Text = cv.Values("price").ToString()
        End If
    End Sub

Session Example:
<tabe>
       <tr><td>Book Name:</td><td><asp:TextBox ID="txtbookname" runat="server"></asp:TextBox></td></tr>
       <tr><td> Price:</td><td><asp:TextBox ID="txtprice" runat="server"></asp:TextBox></td></tr>
       <tr><td>&nbsp;&nbsp;</td><td> <asp:Button ID="btnquery" runat="server" onclick="Button1_Click" Text="Button" /></td></tr>
       </tabe>

On button click write the below mention code:
protected void Button1_Click(object sender, EventArgs e)
    {
        Session["name"] = txtbookname.Text;
        Session["price"] = txtprice.Text;
        Response.Redirect("Next.aspx");
    }

In VB
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnquery.Click
       Session("name") = txtbookname.Text
        Session("price") = txtprice.Text
        Response.Redirect("Next.aspx")
    End Sub

Now on Next.aspx page load write the below mention code:
protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["name"] != null)
        {
            lblbook.Text = Session["name"].ToString();
            lblprice.Text = Session["price"].ToString();
        }
    }

In VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       If Session("name") IsNot Nothing Then
            lblbook.Text = Session("name").ToString()
            lblprice.Text = Session("price").ToString()
        End If
    End Sub

Application Variable Example:
<tabe>
       <tr><td>Book Name:</td><td><asp:TextBox ID="txtbookname" runat="server"></asp:TextBox></td></tr>
       <tr><td> Price:</td><td><asp:TextBox ID="txtprice" runat="server"></asp:TextBox></td></tr>
       <tr><td>&nbsp;&nbsp;</td><td> <asp:Button ID="btnquery" runat="server" onclick="Button1_Click" Text="Button" /></td></tr>
       </tabe>

On button click write the below mention code:
  protected void Button1_Click(object sender, EventArgs e)
    {
        Application["book"] = txtbookname.Text;
        Application["price"] = txtprice.Text;
        Response.Redirect("Next.aspx");
    }

In VB
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnquery.Click
        Application("book") = txtbookname.Text
        Application("price") = txtprice.Text
        Response.Redirect("Next.aspx")
    End Sub

Now on Next.aspx page load write the below mention code:
protected void Page_Load(object sender, EventArgs e)
    {
        if (Application["book"] != null)
        {
            lblbook.Text = Application["book"].ToString();
            lblprice.Text = Application["price"].ToString();
        }
    }

In VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Application("book") IsNot Nothing Then
            lblbook.Text = Application("book").ToString()
            lblprice.Text = Application("price").ToString()
        End If
    End Sub

Debug and check the result.

Is it helpful?


If yes post your comment to admire my work. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

No comments:

Post a Comment