Query String in ASP.Net
Query string is a simple way to pass some information from one page to another. The information cab be easily pass to one page to another or to same page. With query string method the information passed in url of page request.
This method many browsers supports only 255 character length of url in query string. The value passed will be visible so some time it cause security issue.
For send information to other page Response.Redirect() method used and for retrieve information from url use Request.QueryString() method used.
In Query String method we can sent value to only desired page, and value will be temporarily. So Query string increase the overall performance of web application.
Syntax of Query String
Send information to other page
Response.Redirect(“nextpage.aspx?name=value”);
Retrieve information from other page
Request.QueryString[“name”].ToString();
Query String Example in ASP.Net
Design asp.net web form with a button control along with a textbox control. We will pass textbox value to nextpage.aspx using query string method and retrieve information from url in nextpage and display it in label control.
C# code for Query String Example
Write below code on SEND button click events on first page for pass information to other page.
protected void btnsend_Click(object sender, EventArgs e)
{
Response.Redirect(“NextPage.aspx?name=” + txtname.Text);
}
Write below code on Retrieve button for retrieve information from url and display it in label on Nextpage.aspx.
protected void btnretrieve_Click(object sender, EventArgs e)
{
Label1.Text = “Welcome ” + Request.QueryString[“name”].ToString();
}
Pass multiple values using Query String in ASP.Net
In above example we sent single information using query string. If we want to send multiple values in url using query string method, check below example.
Syntax to pass multiple values
Response.Redirect(“NextPage.aspx?name=value1&city=value2”);
Query String Example
C# code for Query String example
Querystrings.aspx page
protected void btnsend_Click(object sender, EventArgs e)
{
Response.Redirect("NextPage.aspx?name=" + txtname.Text + "&city="+txtcity.Text);
}
NextPage.aspx page
protected void btnretrieve_Click(object sender, EventArgs e)
{
Label1.Text = "Name = " + Request.QueryString["name"].ToString();
Label2.Text = "City = " + Request.QueryString["city"].ToString();
}
Related ASP.Net Topics :
Session State in C#.Net
Application State in C#.Net
Subscribe us
If you liked this asp.net post, then please subscribe to our YouTube Channel for more asp.net video tutorials.
We hope that this asp.net c# tutorial helped you to understand about Query String in C#.
Next asp.net tutorial we will understand about Cookies in C#.