Query String in ASP.NET
What is a
QueryString?
A Query String is a value specified
in an HTTP query that can be accessed 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 ampersand(‘&’) or a semicolon(‘;’).
The following is an example of the query string with a field name of ‘id’ and a value of ‘1’: http://www.mywebsite.com/default.aspx?id=1. Query strings can be used for many different reasons, one common use is to display different data on the same page based on the query string.
The following is an example of the query string with a field name of ‘id’ and a value of ‘1’: http://www.mywebsite.com/default.aspx?id=1. Query strings can be used for many different reasons, one common use is to display different data on the same page based on the query string.
For example, if I had an online
store and wanted a page to display an inidividual item from my database on the
page, we could use a query string. This would work by passing something such as
the item’s id in the database as a query string to the page, and then
displaying data from the database based on the value of the query string.
How
to create a Query String ?
You can create a new writeable
instance of HttpValueCollection by calling
System.Web.HttpUtility.ParseQueryString(string.Empty).
NameValueCollection queryString =
System.Web.HttpUtility.ParseQueryString(string.Empty);
queryString["param1"] =
"paramValue1";
How
to retrieve Query String ?
The QueryString collection retrieves
the values of the variables in the HTTP query string and it is specified by the
values following the ? (question mark).
protected void Page_Load(object
sender, EventArgs e)
{
string
param1 = Request.QueryString["param1"];
string
param2 = Request.QueryString["param2"];
}
Useful Link : Top 15 Asp.Net MVC Interview Question with Answers
Advantages of query string :
- All
browsers will support query string and easy to code and use.
- It does
not require any server resource to pass values between pages.
Disadvantages of query string :
Comments
Post a Comment