Blog author: Johnbhatt
| Posted on: 7/19/2012 | Category: ASP.NET Blogs
| Views: 950 | Status: [Member]
| Points: 75
| Alert Moderator
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");
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"
<asp:TemplateField HeaderText="Product Name">
<asp:Label runat="server" ID="PName" Text='<%#Eval("ProductName") %>'></asp:Label>
<asp:TemplateField HeaderText="Price" ItemStyle-Width="100">
<big><b><asp:Label ID="PPrice" runat="server" Text='<%#Eval("ProductPrice") %>'></asp:Label></b></big>
<asp:TemplateField HeaderText="Product Image">
<asp:Image ID="PImage" runat="server" ImageUrl='<%#Eval("ProductImage")%>' Width="150"
<asp:FileUpload ID="FName" runat="server" />
<asp:TemplateField HeaderText="Edit Details">
<asp:LinkButton ID="LnkEdit" runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
<asp:TemplateField HeaderText="Remove Product">
<asp:LinkButton ID="LnkDelete" runat="server" CommandName="Delete" Text="Remove from Cart"></asp:LinkButton>
And now how will we get the Variable Stored in Shopping Page (PIDs) here, lets Clear with code.
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();
GView1.DataSource = ds.Tables;
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.
Glad to Know, Free to Share.....
Found interesting? Add this to: