In this post i am explaining how to use querystrings.
Example url with querystring can be something similar like this
http://yahoo.com/defauld.aspx?variable1=value1&variable2=value2
Suppose we have a textbox txtData and we want it's value on other page
than in code behind we would write in click event of btnGo
Or
Now to retrieve these values on other page we need to use request.querystring, we can either retrieve them by variable name or by index
Or we can also use
QueryString can't be used for sending long data because it has a max lenght limit
Data being transferred is visible in url of browser
To use spaces and & in query string we need to replace space by %20 and & by %26
Or we can use Server.UrlEncode method
Example url with querystring can be something similar like this
http://yahoo.com/defauld.aspx?variable1=value1&variable2=value2
Suppose we have a textbox txtData and we want it's value on other page
than in code behind we would write in click event of btnGo
Or
1
private
void
btnGO_Click(
object
sender, System.EventArgs e)
2
{
3
Response.Redirect(
"Default2.aspx?city="
+
4
txtData.Text +
"&country="
+ txtcountry.Text);
5
}
Now to retrieve these values on other page we need to use request.querystring, we can either retrieve them by variable name or by index
1
private
void
Page_Load(
object
sender,System.EventArgs e)
2
{
3
txtCity.Text = Request.QueryString[
"city"
];
4
txtCountry.Text = Request.QueryString[
"country"
];
5
}
Or we can also use
1
private
void
Page_Load(
object
sender,System.EventArgs e)
2
{
3
txtCity.Text = Request.QueryString[0];
4
txtCountry.Text = Request.QueryString[1];
5
}
QueryString can't be used for sending long data because it has a max lenght limit
Data being transferred is visible in url of browser
To use spaces and & in query string we need to replace space by %20 and & by %26
1
private
void
btnGO_Click(
object
sender, System.EventArgs e)
2
{
3
Response.Redirect(
"Default2.aspx?Value="
+
4
txtData.Text.Replace(
" "
,
"%20"
);
5
}
Or we can use Server.UrlEncode method
1
private
void
btno_Click(
object
sender, System.EventArgs e)
2
{
3
Response.Redirect(
"Default2.Aspx?"
+
4
"Name="
+ Server.UrlEncode(txtData.Text));
5
}
No comments:
Post a Comment