Asp.net programming we consider all important topic for knowledge about controls,web Services, JSON, Ajax, database, and interview related Question. In this Asp.net and sql server tutorial blog we give many examples of jquery as like How Play YouTube Video in Your Asp.net Website by URL, How to Play YouTube Videos in Asp.net by using Jquery , Jquery Change div background on mouseover in asp.net , JQuery UI Datepicker (Calendar) etc.

Friday, August 15, 2014

Asp.net Query String with c# Example

Asp.net Query String :

In this post we see the QueryString technique of sending data from one page two another pages. In the Internet, a querystring is the part of a URL (uniform resource locator) which containing data that does not fit conveniently into a hierarchical path structure. The query string commonly includes data fields added to a base URI Part by a browser or another client web application, for example as part of a HTML form.

Example of QueryString:


The Asp QueryString collection is used to get the variable values in the Hypertext Transfer Protocol (refer to HTTP) query string. The HTTP (Hypertext Transfer Protocol) query string is specified by the values , Here we use the question mark (?) between asp.net web page and first asp.net query string variable, like this:

<a href= "Mypage.asp?ID=1000 ">Link of query string</a>

The line above generates a variable named ID with the value "1000".
Query strings are also created by asp.net web form submission.

Query String Syntax: 

Request.QueryString(variable)[(index)]


Variable refers to ‘Required’ of the variable in the HTTP query string to retrieve. And index is optional. That refers one of multiple values for a Query String variable.
Advantages of QueryString

First Page of Query String in asp Example:

First Query String.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="First Query String.aspx.cs"
    Inherits="First_Query_String" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>QueryString Example in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <b>Query String Example </b>
    </div>
    <br />
    <div>
        <table>
            <tr>
                <td>
                    <b>Id:</b>
                </td>
                <td>
                    <asp:TextBox ID="txtId" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    <b>Name:</b>
                </td>
                <td>
                    <asp:TextBox ID="txtName" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Button ID="btnok" Text="Send" runat="server" OnClick="btnok_Click" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

First Query String.aspx.cs page:

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

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

    }
    protected void btnok_Click(object sender, EventArgs e)
    {

        Response.Redirect("QueryString.aspx?UserId=" + txtId.Text + "&UserName=" + txtName.Text);
    }

}

 Query String in asp.net Example Second page code:

QueryString.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="QueryString.aspx.cs" Inherits="QueryString" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>QueryString Example in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <b>QueryString parameter Values in&nbsp; Second_Query_String.aspx Page</b></div>
    <br />
    <div>
        <b>Id:</b><asp:Label ID="lblUserId" runat="server" /></div>
    <br />
    <div>
        <b>Name:</b><asp:Label ID="lblName" runat="server" /></div>
    </form>
</body>
</html>

QueryString_.aspx.cs:

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

public partial class QueryString : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblUserId.Text = Request.QueryString["Id"];
            lblName.Text = Request.QueryString["Name"];
        }
    }
}


Other Asp.net Related post :



No comments:

Post a Comment