Wednesday, 30 March 2011

Querystring In ASP.NET


Introduction
With the help of the qurrystring we can send the value from one page to another page. This is very important topic of the ASP.NET.


Implementation
Create page named first.aspx . Place there is a textbox and a button at  this page. You can see it in the HTML code

HTML  code for first.aspx page


<body>
    <form id="form1" runat="server">
    <div>
   
        &nbsp;&nbsp; Enter your Name :
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
            Text="send data to next page" />
&nbsp;&nbsp;
   
    </div>
    </form>
</body>

C# code for first.aspx page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class first : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
   // method to send value from querystring

        Response.Redirect("second.aspx?name=" + TextBox1.Text);
    }
}


Create page named second.aspx . Drag and drop a label from toolbox.see HTML code

HTML  code for second.aspx page

<body>
    <form id="form1" runat="server">
    <div>
   
        &nbsp;&nbsp;<asp:Label ID="lbl_msg" runat="server"></asp:Label>
   
    </div>
    </form>
</body>

C#  code for second.aspx page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class second : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
// method to catch value from querystring
        lbl_msg.Text = Request.QueryString["name"].ToString();
    }
}
Conclusion
Through this article, you have learned how to send value from one page to another page using querystring


2 comments: