Skip to main content

Checkbox inside GridView like Gmail


Checkbox inside GridView like Gmail



Even Users want to Check all of the Gridview CheckBox Column for some desired task like Delete, Select Records in GridView. In this Article I am going to describe CheckBox inside Gridview and functionality to check all checkbox in Gridview like Gmail.

I am using JavaScript to Check/UnCheck all CheckBox in GridView. I am using a sample example of Employee Record in Grid to delete the records.

There is a table Employee which I have used in the sample code.

Create Table Employee
(
    Emp_ID int identity(1,1),
    Emp_Name varchar(50),
   Salary money
)

ASPX CODE


<script type="text/javascript">
function SelectAllCheckboxes(chk)
{
    if(chk.type=="checkbox")
       var a=chk;
    State=a.checked;
    elm=a.form.elements;
    for(i=0;i<elm.length;i++)
       if(elm[i].type=="checkbox")
       {     
          if(elm[i].checked!=State)
            elm[i].click();
          else if(elm[i].checked==State)
            elm[i].click();      
       }
}

    </script>

<asp:Button ID="b1" runat="server" Text="DeleteAll" OnClick="b1_Click" />
<br/>

        <asp:GridView ID="GridView1" runat="server" AllowPaging="true" AutoGenerateColumns="false"
            DataKeyNames="Emp_ID" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDeleting="GridView1_RowDeleting">
            <Columns>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <input type="checkbox" id="chkAll" runat="server" onclick="javascript:SelectAllCheckboxes(this)" />
                    </HeaderTemplate>
                    <ItemTemplate>
                        <input type="checkbox" id="chkSelect" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Emp_ID" Visible="false" />
                <asp:TemplateField HeaderText="Employee ID">
                    <ItemTemplate>
                        <asp:Label ID="l1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Emp_ID")%>'>
                        </asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="t1" runat="server" ReadOnly="true" Text='<%DataBinder.Eval(Container.DataItem,"Emp_ID")%>'>
                        </asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Employee Name">
                    <ItemTemplate>
                        <asp:Label ID="l2" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Emp_Name")%>'>
                        </asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="t2" runat="server" Text='<%DataBinder.Eval(Container.DataItem,"Emp_Name")%>'>
                        </asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Salary">
                    <ItemTemplate>
                        <asp:Label ID="l3" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"salary")%>'>
                        </asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="t3" runat="server" Text='<%DataBinder.Eval(Container.DataItem,"salary")%>'>
                        </asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ShowDeleteButton="true" DeleteText="Delete" HeaderText="DELETE" />
            </Columns>
        </asp:GridView>

ASPX.CS CODE

SqlConnection con = new SqlConnection("server=NEERAJ-PC;database=neeraj;uid=sa;pwd=sa");
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            display();
    }
    public void display()
    {
        da = new SqlDataAdapter("select * from Employee", con);
        ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        display();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        bool isChecked = ((HtmlInputCheckBox)GridView1.Rows[e.RowIndex].FindControl("chkSelect")).Checked;
        if (isChecked)
        {
            string Emp_ID = ((Label)GridView1.Rows[e.RowIndex].FindControl("l1")).Text;
            int id = int.Parse(Emp_ID);
            cmd = new SqlCommand("delete from Employee where Emp_ID=" + id, con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            GridView1.EditIndex = -1;
            display();
        }
    }
    protected void b1_Click(object sender, EventArgs e)
    {
        string ids = "";
        ids = GetSelectedIds();
        con.Open();
        SqlCommand cmd = new SqlCommand("delete from Employee where Emp_ID in(" + ids + ")", con);
        cmd.ExecuteNonQuery();
        display();

    }
    private string GetSelectedIds()
    {
        string ids = "";
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            bool isChecked = ((HtmlInputCheckBox)row.FindControl
                ("chkSelect")).Checked;
            if (isChecked)
            {
                string s = ((Label)row.FindControl("l1")).Text;
                ids = ids + s + ",";
            }

        }
        return ids.Substring(0, ids.Length - 1);
    }


Now run the application and see the result as given below image





Powered by/ refrence from: http://www.dotnetfactor.com/Article/checkbox-inside-gridview-like-gmail-22.aspx

Comments

  1. Are you trying to make cash from your visitors via popup ads?
    In case you do, have you considered using Propeller Ads?

    ReplyDelete

Post a Comment

Popular posts from this blog

10 jQuery Custom Scrollbar Plugins

10 jQuery Custom Scrollbar Plugins If you ever wanted to add some custom scrollbars to your website, to scroll the contents and the default browser scrollbars just doesn’t match up with your design, than make sure you check this list of 10 jQuery custom scrollbar plugins. Hope you find the following information helpful. 1. jScrollPane – custom cross-browser scrollbars Kelvin Luck’s jScrollPane was originally developed in December 2006. It is a jQuery plugin which provides you with custom scrollbars which work consistently across all modern browsers. You can style the scrollbars using simple CSS and they degrade gracefully where JavaScript is disabled. 2. Plugin JQuery : Scrollbar This page is written in french so use Google’s translate service to translate this page to your preferred language. Download is available for the plugin.  The purpose of this plugin is to add a scrollbar to the item of your choice, to view any content which is larger than the size – vizibl...

Contact us Forms Links

Hi Friends This is my second post to blogger.com. and this time I am going to share my few useful resource links with you all. This post is regarding to “How to create contact us form to the html page” So please go to the following sites 1) http://wufoo.com/gallery/templates/forms/contact-form/ Thats it
Hello Friends This is an important moment for me! This is the first blog I’ve ever written. It’s something I’ve wanted to do for a long time, but wasn’t sure how to do it, where, when, or even if. And finally, now here I am. People say that blogs are like a diary (do I hide it under my bed, lock it with a key? Tell it all my secrets? It seems like a wonderful place to share my thoughts with you, or what I do.) It’s going to be fun sharing my thoughts with you, outside the context of a book. Ok let me tell you about myself. I am a I.T Engineer and having 2 year experience about the I.T Sector. After Completion of my graduation i worked as a "Web Designer" for 1.3 years then i Switch my profile to software engineer and I am working as a Software engineer from 6 months. so basically my blogs are all bout to the new technologies, interesting and important codes and some useful resources. For more about myself please visit my website: http://www.lokeshchadha.co.cc/ . 1)..Downl...