Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 54234 |  Welcome, Guest!   Register  Login
 Home > Blogs > ASP.NET > State Management in ASP.NET part-2 (QueryString) ...
Johnbhatt

State Management in ASP.NET part-2 (QueryString)

 Blog author: Johnbhatt | Posted on: 7/19/2012 | Category: ASP.NET Blogs | Views: 950 | Status: [Member] | Points: 75 | Alert Moderator   
Ads

Hi, 
Welcome to State Management in ASP.NET Series.

Today I am back with QueryString, another Client Based State Management concept.

Suppose, you are Creating a Website with Shopping Cart. In shopping page there are generally two or more forms. One for Product Selection (store) and another for Payment Gatebay (Cart/Checkout). 

Suppose you displayed your Product in Store using DataList or GridView or any method that you prefer. Now how do you pass the Values from Store to Checkout Page? As We know HTTP is Stateless protocol so all your Variable values are lost during postback.  Lets be clear with below example.

 This is a example of Store. all products are listed here. To buy a check box is placed below every product. When you are done shopping, now for Payment options we have to move to Cart. So this page has a button named Add to Cart. Now question is same, How do you get details of Checked (selected) products in Next Page as the data is coming from Database. 

To solve this type of Problem, Microsoft has created concept of QueryString in ASP.NET. Look at below code which Add to Cart button click event in ASPX.CS Page.
 
   protected void BtnBuy_Click(object sender, EventArgs e)
    {
        string PIDs = "";
        for (int i = 0; i <= DList1.Items.Count - 1; i++)
        {
            CheckBox chkbuy = (CheckBox)DList1.Items[i].FindControl("ChkBuy");
            if (chkbuy.Checked)
            {
                Label lblPCode = (Label)DList1.Items[i].FindControl("PCode");
                PIDs += lblPCode.Text + ",";
            }
        }
        PIDs = PIDs.Remove(PIDs.Length - 1);
        Response.Redirect("CartView.aspx?PCode=" + PIDs);
 
     }
Here I made a string of Product Key's and Passed that inside Response.Redirect by adding ?PCode=123,1233 etc.

Which looks like in Below Example:


Here PIDs is a variable of string type which is Holding All Product Keys (Primary keys) of Selected Products. 

Now we are in New Page lets look what this will display and how we will get the Key. Source of Design.

  <asp:GridView ID="GView1" runat="server" AllowPaging="true" AllowSorting="true" PageSize="3"
        AutoGenerateColumns="false" OnRowDeleting="GView1_RowDeleting" 
            OnRowEditing="GView1_RowEditing">
        <Columns>
            <asp:TemplateField HeaderText="Product Name">
                <ItemTemplate>
                    <asp:Label runat="server" ID="PName" Text='<%#Eval("ProductName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <Columns>
            <asp:TemplateField HeaderText="Price" ItemStyle-Width="100">
                <ItemTemplate>
                    <big><b><asp:Label ID="PPrice" runat="server" Text='<%#Eval("ProductPrice") %>'></asp:Label></b></big>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>

        <Columns>
            <asp:TemplateField HeaderText="Product Image">
                <ItemTemplate>
                    <asp:Image ID="PImage" runat="server" ImageUrl='<%#Eval("ProductImage")%>' Width="150"
                        Height="150" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:FileUpload ID="FName" runat="server" />
                </EditItemTemplate>
            </asp:TemplateField>
        </Columns>
        <Columns>
            <asp:TemplateField HeaderText="Edit Details">
                <ItemTemplate>
                    <asp:LinkButton ID="LnkEdit" runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <Columns>
            <asp:TemplateField HeaderText="Remove Product">
                <ItemTemplate>
                    <asp:LinkButton ID="LnkDelete" runat="server" CommandName="Delete" Text="Remove from Cart"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

And now how will we get the Variable Stored in Shopping Page (PIDs) here, lets Clear with code.

 void BindDList()
    {
        string PIDs = Request["PCode"].ToString();
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyShop"].ConnectionString);
        SqlDataAdapter adp = new SqlDataAdapter("Select * from Products where ProductID in (" + PIDs + ")", con);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        GView1.DataSource = ds.Tables[0];
        GView1.DataBind();
    }
Here the method Request[QueryString] is used to get String and value which we passed as ?PCode= is Query string here. 

You can check at your machine and revert in case of any sort of errors.

Happy Coding.



John Bhatt
Glad to Know, Free to Share.....
http://www.johnbhatt.com
Found interesting? Add this to:


About John Bhatt

Experience:4 year(s)
Home page:http://www.johnbhatt.com
Member since:Thursday, June 21, 2012
Level:Starter
Status: [Member]
Biography:John Bhatt is an IT Professional having interest in Web technology. He is Web Designer, Developer, Software Developer, Blogger and Technology Geek. Currently he writes his Blogs at Blog of P.Yar.B and various other Sites. He is Main author and founder of Download Center. Contact Him at : Facebook | Twitter | Website.
>> Write Response - Respond to this post and get points

More Blogs

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/19/2013 11:43:32 AM