Monday, August 13, 2012

QueryString Example in ASP.NET

In this post i will explain how to pass values between aspx pages.


Create 2 pages as shown below.
Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Home Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="btnitempage" runat="server" Text="Go to Item Page" 
            onclick="btnitempage_Click" />
    </div>
    </form>
</body>
</html>

OutPut


Items.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Items Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="lblvaluepassed" runat="server" Text="Category Code is :"></asp:Label>
    <asp:Label ID="lblItemnum" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

Code for Default.cs page
On Button Click add the following code

 protected void btnitempage_Click(object sender, EventArgs e)
    {
        string CategoryCode = "ab2012";
        Response.Redirect("Items.aspx?cat=" + CategoryCode);
    }

Code for Items.cs page 

 Page Load get the values

protected void Page_Load(object sender, EventArgs e)
    {
        string catcode = Request.QueryString["cat"].ToString();
        lblItemnum.Text = catcode;
    }

Value from the page URL is shown in label below.





No comments:

Post a Comment