asp.net c# tutorials and examples code for web developers

How to use QueryString in asp.net

QueryString in asp.net
QueryString is an alternate and popular way to passing variables between web pages. one page can pass variables with values and another web page can retrieve the variables and its values. we can declare query string variables in an anchor element. we also can pass one variable or multiple variables at a time within an anchor element (html link). to pass multiple variables using querystring we need to use '&' separator between variables. each variable contains its value that we need to assign by an equal '=' sign. this is an example url to pass variables using query string.

http://www.asp.net/Image.aspx?ImageID=1&ImageName=Elephant

in the above example url, we separated the original url and querystring by a question mark '?' separator. then we declare a variable name ID and assing its value to 1 by using an equal '=' sign. we also added another variable name 'ImageName' and its value 'Elephant'. here we separated and combined to variables in querystring using '&' sign.

we can retrieve those querystring variables in another page where the link navigate to. in the above example url we can read the query string variables in Image.aspx web page. HttpRequest.QueryString property get the collection of http query string variables. the following example code describe us how can we read the above query string variables.

string ImageID = Request.QueryString["ID"];
string ImageFullName = Request.QueryString["ImageName"];


if the url does not have a 'ImageName' querystring variable then the return value would be null. if we want to use spaces and '&' in query string we need to replace 'space' by %20 and '&' by %26.

the following asp.net c# example code demonstrate us how can we use qurystring in an asp.net application.
QueryString.aspx
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>asp.net QueryString example: how to use QueryString</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">QueryString Example</h2>
        <asp:HyperLink 
            ID="HyperLink1"
            runat="server"
            NavigateUrl="~/Image.aspx?ImageID=1&ImageName=Elephant"
            Text="Test QueryString"
            >
        </asp:HyperLink>
    </div>
    </form>
</body>
</html>
Image.aspx
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e) {
        string ID = Request.QueryString["ImageID"];
        string Name = Request.QueryString["ImageName"];
        Label1.Text = "ImageID: "+ ID;
        Label2.Text = "Image name: "+ Name;
        Image1.ImageUrl = "~/Images/"+Name+".jpg";
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>asp.net QueryString example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Red">QueryString Example: Image View</h2>
        <asp:Label 
            ID="Label1"
            runat="server"
            Font-Size="Large"
            ForeColor="SeaGreen"            
            >
        </asp:Label>
        <br />
        <asp:Label 
            ID="Label2"
            runat="server"
            Font-Size="Large"
            ForeColor="SeaGreen"            
            >
        </asp:Label>
        <br />
        <asp:Image ID="Image1" runat="server" />
    </div>
    </form>
</body>
</html>
Related asp.net example