Click here to Skip to main content
Click here to Skip to main content
Go to top

Select multiple checkboxes in a GridView in online shopping websites

, 12 Jul 2013
Rate this:
Please Sign up or sign in to vote.
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:

<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:

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)

Share

About the Author

Deenuji
Software Developer (Junior) Zylog Systems Ltd
India India
I am a normal man!!!! Is not like what u think !!!!!!
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralMy vote of 4 Pinmemberfredatcodeproject13-Jul-13 5:02 
GeneralRe: My vote of 4 PingroupDeenuji13-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 | Mobile
Web02 | 2.8.140905.1 | Last Updated 12 Jul 2013
Article Copyright 2013 by Deenuji
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid