Passing variables between pages using QueryString


sometimes we need to maintain state between pages
that means we need to use values in one page in other pager for that we need to pass one page values to another page: for that we use different techniques
1.Querystring
2.sessions
3.cookies
4.viewstate
etc…
now we will learn how to pass values using query string…
first prepare form1 as follows
——————————-
take one textbox and one button
when we click on button the textbox value we need see in form2

To send One Value:

for this we need to take one text box in Form1

<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>

and one button

<asp:Button ID=”Button1″ runat=”server” onclick=”Button1_Click” Text=”Send” />

write the following code in button click event

Response.Redirect(“Form2.aspx?name=”this.TextBox1.Text);

then write the following code in Form2 PageLoad event

Response.Write(Request.QueryString[“name”]);

To send Two Values:

for this we need to take two textboxes in Form1

<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>

<asp:TextBox ID=”TextBox2″ runat=”server”></asp:TextBox>

and one button

<asp:Button ID=”Button1″ runat=”server” onclick=”Button1_Click” Text=”Send” />

write the following code in button click event

Response.Redirect(“Form2.aspx?name=”this.TextBox1.Text + “&lastname=” + this.TextBox2.Text);

then write the following code in Form2 PageLoad event

Response.Write(Request.QueryString[“name”]);

Response.Write(Request.QueryString[“lastname”]);

Another Way to Read values(Using for loop):

write the following code in Form2 PageLoad event

for (int i = 0; i < Request.QueryString.Count;i++ )
Response.Write(Request.QueryString[i]);

We can assign those values to textboxes in Form2 also:

Now take two textboxes in form2 and write the following code in Form2 PageLoad event

string str1 = Request[“Name”];
string str2 = Request[“lastname”];
TextBox1.Text = str1;
TextBox2.Text = str2;

Replace “space” and “&” with %20 and %26 using UrlEncode:

Observe the google Querystring it replace space by %20 and & by %26

For that we need to following

string s1 = this.TextBox1.Text.Replace(“&”, “%26”);
string s2 = this.TextBox2.Text.Replace(“&”, “%26″);
string str1 = s1.Replace(” “, “%20″);
string str2 = s2.Replace(” “, “%20”);
string url = “Form2.aspx?name=” + Server.UrlEncode(str1) + “&last=” + Server.UrlEncode(str2);
Response.Redirect(url);

————————————————————————————————-

Advertisements

3 thoughts on “Passing variables between pages using QueryString

  1. I have one requirement like this
    I have a table CUSTOMER in that CUSTID,NAME,URL columns is thier
    CUSTID NAME URL
    1 asp http://www.asp.net
    2 weblog http://www.weblogs.asp.net
    3 google http://www.google.com

    My question is I opening perticular url based on CUSTID

    I am Passing parameter Like http://www.Example.com?CUSTID=1 That

    corressponding URL data(Ex:www.asp.net) It will open directly in browser

    using asp.net……..
    plz help me ………..
    Thank you,
    anil

      • Page1.aspx

        .cs
        protected void Page_Load(object sender, EventArgs e)
        {
        SqlConnection con = new SqlConnection(@”Data Source=;Initial Catalog=productdb;Persist Security Info=True;User ID=;Password=”);

        SqlDataAdapter da = new SqlDataAdapter(“select * from redirecturl”, con);

        DataSet ds = new DataSet();
        da.Fill(ds, “redirecturl”);
        QS1.DataSource = ds.Tables[“redirecturl”];
        QS1.DataBind();
        QS1.DataKeyNames = new string[] { “Tagid” };
        }
        protected void GvDept_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
        QS1.SelectedIndex = e.NewSelectedIndex;

        string Dno = QS1.SelectedValue.ToString();
        Response.Redirect(“~/Page2.Aspx?TagId=”+Dno);

        }
        Page2.aspx

        .cs
        String URL = “”;
        protected void Page_Load(object sender, EventArgs e)
        {
        string Dno = Request.QueryString[“TagId”];

        SqlConnection con = new SqlConnection(@”Data Source=;Initial Catalog=productdb;Persist Security Info=True;User ID=;Password=”);

        SqlCommand cmd = new SqlCommand(“select coupounurl from redirecturl where Tadid=” + Dno, con);

        con.Open();

        SqlDataReader dr = cmd.ExecuteReader();
        QS2.DataSource = dr;
        QS2.DataBind();
        dr.Close();

        object drReader = cmd.ExecuteScalar();
        if (drReader != null)
        {
        URL = drReader.ToString();
        }

        con.Close();
        if (!String.IsNullOrEmpty(URL))
        {
        //Use Iframe if it works
        ifrmBrs.Attributes[“src”] =URL;
        }
        }
        I am using frame but not open google website
        Tagid coupounurl
        1 https://www.google.co.in/
        2 http://page5.aspx
        3 http://page6.aspx
        Based on tag id Redirect perticular url,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s