Wednesday, June 18, 2008

Query String Asp .Net : Working with multiple values

Query string is the part of URL that can be used to send data as parameters. Let's first analyze the query string generated by Google on making a search for 'Shalvin'

http://www.google.co.in/search?hl=en&q=Shalvin.Here two values are passed as query string.

Create a Web site with two web pages.
In the button click of Default.aspx write the following code:

protected void sumButton_Click(object sender, EventArgs e)
{ Response.Redirect("Sum.aspx?Val1=" + txtVal1.Text + "&Val2=" + txtVal2.Text); }

Here is the code for extracting the value in the second page.
protected void Page_Load(object sender, EventArgs e)
{
	Response.Write("Hello " + Request.QueryString["Name"] + " your blog is " + Request.QueryString["Blog"]);
}
 Calculator

<body>
    <form id="form1" runat="server">
    <div>
    
        Value 1<asp:TextBox ID="txtVal1" runat="server"></asp:TextBox>
        <asp:CompareValidator ID="CompareValidator1" runat="server" 
            ControlToValidate="txtVal1" ErrorMessage="Value cannot be string" 
            Operator="DataTypeCheck" Type="Integer"></asp:CompareValidator>
        <br />
        <br />
        Value 2<asp:TextBox ID="txtVal2" runat="server"></asp:TextBox>
        <asp:CompareValidator ID="CompareValidator2" runat="server" 
            ControlToValidate="txtVal2" ErrorMessage="Value cannot be string " 
            Operator="DataTypeCheck" Type="Integer"></asp:CompareValidator>
        <br />
        <br />
        <asp:Button ID="sumButton" runat="server" onclick="Button1_Click" Text="Button" />
    
    </div>
    </form>
</body>


Default.aspx

protected void Button1_Click(object sender, EventArgs e)
{ Response.Redirect("Sum.aspx?Val1=" + txtVal1.Text + "&Val2=" + txtVal2.Text); }

Sum.aspx
int i, j, res;
protected void Page_Load(object sender, EventArgs e)
{
    i = Int32.Parse(Request.QueryString["Val1"]);
    j = Int32.Parse(Request.QueryString["Val2"]);
    res = i + j;
    Response.Write(res.ToString());
}


Related Blog

ASP.Net 2.0 Profile

Session Asp .Net : Storing DataSet

No comments:

Post a Comment