This blog explains -
- What is QueryString?
- Why do we use QueryString?
- How to pass the values through QueryString?
- How to retrieve the values from QueryString?
QueryString
- Query string is one of the technique to send data from one webform to another through URL.
-
Query string consist of two parts (field and value), and each of pair separated by ampersand (&).
-
?(Question Mark), indicates the beginning of a query string and it's value.
-
There is a limit on the Query string length. Hence, Query strings cannot be used to send very long data.
-
Query strings are visible to the user, hence should not be used to send sensitive information (Like Username, Password), unless encrypted.
-
To retrieve the query string value, use Request object's QueryString property.
Lets understand with an example:
- To create the project -
- Go to Start -> then All Programs and click Microsoft Visual Studio 2010
- Go to File -> New -> Project..., Visual C# , Web. Then select ASP.NET Empty Web Application.
- Provide the project a name and specify the location.
Next: Right-click on Solution Explorer and add a two web forms (user.aspx, Mas_Employee.aspx) to your project.
Send the Values using QueryString :
Design your user.aspx as in the following.
Copythe following code in your button click event to send multiple query string values (Name, DeptNmae) through URL from user.aspxpage to Mas_Employee.aspx.
- protected void btnSend_Click(object sender, EventArgs e)
- {
- Response.Redirect("/Application /Mas_Employee.aspx?Name=" + txtName.Text + "&DeptName=" + txtDeptName.Text);
- }
Retrieve the values from QueryString:
Design your Mas_Employee.aspx as in the following
To retrieve the QueryString Values (Name, DeptNmae) in Mas_Employee.aspx copy the following code in your Mas_Employee.aspx.cs.
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- if (Request.QueryString["Name"] != null && Request.QueryString["Name"] != string.Empty)
- lblName.Text = Request.QueryString["Name"];
-
- if (Request.QueryString["DeptName"] != null && Request.QueryString["Name"] != string.Empty)
- lblDeptName.Text = Request.QueryString["DeptName"];
- }
- }
Run the application and Provide Name and DeptName values and then click on Send. At this point you will get the following result.
Output:
Conclusion
QueryString are used to pass the data (limited data through URL) from one page to another page . For example. If we want to send one page control values to another page you can use QueryString.
I hope you enjoyed it. Please provide your valuable suggestions and feedback if you found this blog helpful.