Contents

Thursday, 7 July 2016


Query String

Query String is used to send data from one page to another page through URL. The Query String are used to start with a "?" sign. If we want to add more Query String we use "&" sign.

Query Strings are mainly used to send small data across pages. In ASP.NET, to move from one page to another we can use Response.Redirect(It is one of the page navigation technique) which redirects the current page to the specified page.Response.Redirect takes URL to specify the path of the new page and with the URL we can pass the Query String and can code to access the value in another page.

I will discuss all the navigation techniques later. Let us stick to the Response.Redirect for now.

Let us take an example to make it clear. We will take the same example which I took for Cookies and ViewState variable. So we will take input name and blog name from the user and will try to move these details to another page and then display it on the new page.

Let us take the name of first page as Page1.aspx and second one as Page2.aspx.

Design

Page1.aspx


Page2.aspx



Code Behind

Page1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace httpstateless
{
    public partial class Page1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //Redirecting to Page2.aspx and sending the values with it
            Response.Redirect("Page2.aspx?Name="+TextBox1.Text+"&Blog Name="+TextBox2.Text);
        }
    }
}


Page2.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace httpstateless
{
    public partial class Page2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Accessing the values send by Page1.aspx
            Label1.Text = Request.QueryString["Name"];
            Label2.Text = Request.QueryString["Blog Name"];
        }
    }
}

Output

Output before clicking the redirect button(current page is Page1.aspx):



Output after clicking the redirect button(loads the Page2.aspx):





Now in the above code when we send the data with the URL we first used "?" sign and gave a name to it and then use an "=" sign. After that a "+" sign and pass the TextBox1 value to the name which we used. To pass more value we have to use & and then the name. After sending the data we access it by Request.QueryString in the new page.

Points to Remember

  • It is generally used to send data from one page to another with URL.
  • It is not suitable for storing large data.
  • It is not suitable to send confidential data like passwords as the new page's URL will also contain the text send by us when we redirect to that page(you refer the Page2 output as it reflects the same).

You may click the Content tab to see all the posts that i will cover in this tutorial for easy navigation.


No comments:

Post a Comment