Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Select multiple checkboxes in a GridView in online shopping websites

4.60/5 (3 votes)
12 Jul 2013CPOL1 min read 13.5K  
Select multiple checkboxes in a GridView in an online shopping website.

Introduction

In this article I will explain how to select multiple checkboxes in a GridView for use in online shopping websites.

CheckBox control in GridView

I have a GridView in my online shopping website and I want to select multiple checkboxes in the GridView. After selecting a checkbox I want to buy the selected items from the online shopping site. To achieve these functionalities I have a write - button click event code:

My HTML coding:

XML
<table style="width: 100%">
  <td style="width: 504px">
  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            Height="298px" Width="593px" 
            onselectedindexchanged="GridView1_SelectedIndexChanged" 
            onrowdatabound="GridView1_RowDataBound" 
            BackColor="#CCCCCC" BorderColor="#999999" 
            BorderStyle="Solid" BorderWidth="3px" 
            CellPadding="4" CellSpacing="2" 
            ForeColor="Black">
        <RowStyle BackColor="White" />
        <Columns>
            <asp:BoundField HeaderText="ID" DataField="ID" />
            <asp:BoundField HeaderText="Product Name" DataField="ProductName" />
            <asp:BoundField HeaderText="Price" DataField="price" />
            <asp:BoundField HeaderText="Category" DataField="category" />
            <asp:TemplateField HeaderText="Product">
             <ItemTemplate>
             <asp:Image width="40" Height="40" 
               ImageUrl='<%#Eval("path")%>' 
               runat ="server"  ID="image1" />
             </ItemTemplate>   
             </asp:TemplateField>
  <asp:TemplateField HeaderText="Buy">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#CCCCCC" />
        <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
    </asp:GridView>
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td style="width: 504px">
   
    
                <asp:Button ID="Button1" runat="server" 
                    Height="31px" onclick="Button1_Click1" 
                    style="font-weight: 700; font-family: Sylfaen; background-color: #DCDBDC" 
                    Text="Buy" Width="83px" />
            </td>
        </tr>
    </table>
//

Button click event code to select multiple checkboxes:

C#
protected void Button1_Click1(object sender, EventArgs e)
{
    string a = Request.QueryString["var"];
   
    foreach (GridViewRow gvr in GridView1.Rows)
    {
        if (gvr.RowType == DataControlRowType.DataRow)
        {
            CheckBox cb = (CheckBox)(gvr.FindControl("CheckBox1"));
            if (cb.Checked == true)
            {  
                SqlConnection cn = new SqlConnection(
                  "Data Source=xxxxxx; Initial Catalog=rule; User Id=sa; Pwd=xxxxx");
                cn.Open();
                SqlCommand cmd = new SqlCommand("Insert into Trans values('" + 
                  Session["count"].ToString() + "','" + gvr.Cells[1].Text + "','" + 
                  gvr.Cells[2].Text + "','" + gvr.Cells[3].Text + "','" + 
                  a + "')", cn); //Write Your Query to insert into database table        
        
                int iCount = cmd.ExecuteNonQuery(); 
                
                cn.Close();
            }
        }
    }
    Response.Write("<SCRIPT>alert('Items are added to cart successfully!!!! ')</SCRIPT>");  
}

After clicking the Buy button a messagebox will appear on the screen and your items are added to the cart successfully!!!!.

How it works

In the above code we have used C#. We have a foreach loop over the Controls collection of the Panel control that provides the reference to each child control in the GridView. Inside the loop, we have an "if condition" to check if a control is a checkbox or not. If it returns true then it will execute the C# code placed inside the "if condition" block that will set the checked state of the referenced checkbox control to the checked state.

After completing this condition loop it goes to the script and appears on the message box on the window....

That is it. I hope you like the article, happy coding!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)