Saturday 4 August 2012

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

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.

No comments:

Post a Comment

Popular posts