QueryString
During the visitor's transition from one page to another on our website or when the page post back to the server, we may want to store some information and use it again when necessary. In such cases state management objects are used.
QueryString is one of these objects and allows us to carry the requested information by adding it to the address (url) of the page.
www.btdersleri.com/ders.aspx?id=148
For example, if you look at the url above, you will see that ?id=148 has been added to the end of the address.
? After the character, the information to be carried with the querystring is written, which consists of two parts, a parameter and a value. Here parameter is id and value is 148.
If more than one parameter and value is to be moved, the & character is put between them.
Örnek: ders.aspx?q1=computer&q2=laptop&q3=game
Sending Data Using QueryString
We can add and send data to QueryString with a simple link. The type of information to be sent is string. If we send numbers or text, they are not enclosed in quotation marks.
<a href="read.aspx?id=1057">Go</a>
The same can be done with C# code. Let's create a page and send the information entered in textBox1 to the preference.aspx page when the button is clicked.
protected void Button1_Click(object sender, EventArgs e)
{
string a = TextBox1.Text;
Response.Redirect("preference.aspx?id=" + a + "");
}
Reading Data in QueryString
Request.QueryString method is used to read the data in querystring.
Usege:
Request.QueryString[ index-number or parameter-name ]
If the parameter name of the information moved in square brackets is to be written, it is written in quotation marks.
If the index number is to be used, it should be noted that the number of the first element will start from zero.
Now let's write the following codes in the Page_Load event of the preference.aspx page. In this way, it is possible to read the parameter named "Data" sent from the previous page and perform the desired action. The data taken in the example is written in the label.
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Request.QueryString["id"];
}
Reading can also be done using the index numbers of the parameters in the QueryString.
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Request.QueryString[0];
}
what is querystring, examples of querystring usage, how to use response redirect, request querystring, Data storage in asp.net address bar
EXERCISES
There are no examples related to this subject.
Read 901 times.