Query String in Asp.Net

Query String :

A query string in Asp.Net  is a value specified in an HTTP query that can be used easily within ASP.NET. The query string is appended at the end of the URL following the question mark(‘?’) character. Multiple query strings can be specified in the URL by separating them by either an (‘&’) or a  (‘;’). Query strings can be used for many different levels, one common use is to display different information on the same pages based on the query string.

Free PDF Download: Latest Updated ASP.NET MVC Topics

For example, if I had an online purchase and wanted a page to display an individual item from my database on the page, we could use a query string.  In this method, it would access by passing data that is information on the page such as the item’s id in the database as a query string to the page, and then display data from the database based on the value of the Query string.

Trending Now: Advanced Features of .NET Framework

Syntax :

Request.QueryString(variable)[(index)|.Count]

  • Variable means required.The name of the variable in the HTTP query string to retrieve
  • Index describes Optional.Specifies one of the multiple values for a variable. From 1 to Request.QueryString(variable).Count

Using QueryStrings :

You can access the query strings of a given page in either the front end or back end of the code. To demonstrate how to do this, you will create a simple website. First, create a new Asp.Net Empty Web Site and then:

Right click the project in your solution explorer.

  • Select add new item…
  • Select a web form.
  • Name it ‘Default.aspx’.
  • Click add.
  • Open Default.aspx.cs up for editing.

Send the Values Using Querystring  :

Write the following code in your button click event to send multiple query string values (UserName, DepartmentName) through URL from user.aspxpage to Mas_Employee.aspx.

protected void btnSend_Click(object sender, EventArgs e)
{

Response.Redirect(“/Application /Mas_Employee.aspx?UserName=” + txtuserName.Text + “&DepartmentName=” + txtDepartmentName.Text);
}

Conclusion :

QueryString is 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.