Click here to Skip to main content
11,804,574 members (91,390 online)
Click here to Skip to main content

Select multiple checkboxes in a GridView in online shopping websites

, 12 Jul 2013 CPOL 6.6K 6
Rate this:
Please Sign up or sign in to vote.
Select multiple checkboxes in a GridView in an online shopping website.


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:

<table style="width: 100%">
  <td style="width: 504px">
  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            Height="298px" Width="593px" 
            BackColor="#CCCCCC" BorderColor="#999999" 
            BorderStyle="Solid" BorderWidth="3px" 
            CellPadding="4" CellSpacing="2" 
        <RowStyle BackColor="White" />
            <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">
             <asp:Image width="40" Height="40" 
               runat ="server"  ID="image1" />
  <asp:TemplateField HeaderText="Buy">
<asp:CheckBox ID="CheckBox1" runat="server" />
        <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" />
            <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" />

Button click event code to select multiple checkboxes:

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");
                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(); 
    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!


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


About the Author

Software Developer Zylog Systems Ltd
India India
I am a normal man!!!! Is not like what u think !!!!!!

You may also be interested in...

Comments and Discussions

GeneralMy vote of 4 Pin
fredatcodeproject13-Jul-13 5:02
memberfredatcodeproject13-Jul-13 5:02 
GeneralRe: My vote of 4 Pin
Deenuji13-Jul-13 19:49
groupDeenuji13-Jul-13 19:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151002.1 | Last Updated 12 Jul 2013
Article Copyright 2013 by Deenuji
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid