|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionOften you need to pass variable content between your html pages or aspx webforms in context of Asp.Net. For example in first page you collect information about your client, her name and last name and use this information in your second page. For passing variables content between pages ASP.NET gives us several choices.
One choice is using
This html addresses use
As you have guessed ? starts your Put this code to your submit button event handler. private void btnSubmit_Click(object sender, System.EventArgs e)
{
Response.Redirect("Webform2.aspx?Name=" +
this.txtName.Text + "&LastName=" +
this.txtLastName.Text);
}
Our first code part builds a query string for your application and send
contents of your textboxes to second page. Now how to retrieve this values from
second page. Put this code to second page private void Page_Load(object sender, System.EventArgs e)
{
this.txtBox1.Text = Request.QueryString["Name"];
this.txtBox2.Text = Request.QueryString["LastName"];
}
private void Page_Load(object sender,
System.EventArgs e)
{
this.txtBox1.Text = Request.QueryString[0];
this.txtBox2.Text = Request.QueryString[1];
}
Some other ways to reach contents of foreach( string s in Request.QueryString)
{
Response.Write(Request.QueryString[s]);
}
Or for (int i =0;i < Request.QueryString.Count;i++)
{
Response.Write(Request.QueryString[i]);
}
Advantages of this approach
Disadvantages of this approach
If you write this code and try them you will see that you have a problems with space and & characters, e.g. if you need to send a variable which contains & such as "Mark & Spencer". There must be a solution for this problem. If you look to Google’s query string you will see that it contains a lot of %20. This is the solution of our third disadvantage. Replace space with %20 and & with %26 for example. private void btnSubmit_Click(object sender, System.EventArgs e)
{
string p1 = this.txtName.Text.Replace("&","%26");
p1 = this.txtName.Text.Replace(" ","%20");
string p2 = this.txtLastName.Text.Replace("&","%26");
p2 = this.txtName.Text.Replace(" ","%20");
"WebForm2.aspx?" +
"Name=" + p1 +
"&LastName=" + p2;
Response.Redirect(p2);
}
Since this is a such a common problem Asp.Net should have some way to solve.
There it is private void btnSubmit_Click(object sender, System.EventArgs e)
{
Response.Redirect("WebForm2.Aspx?" +
"Name=" + Server.UrlEncode(this.txtName.Text) +
"&LastName=" + Server.UrlEncode(this.txtLastName.Text));
}
Same solution is in Microsoft .Net Quick Start tutorials. ASP.NET --- Working with Web Controls ---
Look at them also if you want to see more example for this technique. Also I
advise you to look at Alex Beynenson's article about building
|
||||||||||||||||||||||