Check/Un-Check checkboxes items in ASP.NET DataGrid using JavaScript

In this article we will see how to check and un-check checkboxes items in ASP.NET DataGrid using JavaScript. We will also see how to delete all checked items and to change Datagrid row background color on mouse over.
 
 
Code:

<asp:DataGrid ID="MyDataGrid" AutoGenerateColumns="false" runat="server"
           AllowPaging="true" PagerStyle-Mode="NextPrev"
           onitemdatabound="MyDataGrid_ItemDataBound" ShowFooter="true"
           onpageindexchanged="MyDataGrid_PageIndexChanged" PageSize="4"
           onitemcommand="MyDataGrid_ItemCommand" Width="100%">
<Columns>
  <asp:BoundColumn HeaderText="CustomerId" DataField="CustomerId"></asp:BoundColumn>
  <asp:BoundColumn HeaderText="Name"  DataField="Name"></asp:BoundColumn>
  <asp:BoundColumn HeaderText="City"  DataField="City"></asp:BoundColumn>
  <asp:BoundColumn HeaderText="Mobile"  DataField="Mobile"></asp:BoundColumn>
  <asp:BoundColumn HeaderText="Address"  DataField="Address"></asp:BoundColumn>
  <asp:BoundColumn HeaderText="Date Of Birth" DataFormatString="{0:MM/dd/yyyy}" DataField="DOB"></asp:BoundColumn>
  <asp:TemplateColumn>
      <ItemTemplate>
        <asp:CheckBox ID="cbRows" runat="server"/>
      </ItemTemplate>
      <HeaderTemplate>
        <input type="checkbox" id="mainCB" οnclick="javascript:CheckAll(this);" />
      </HeaderTemplate>
      <FooterTemplate>
        <asp:Button ID="btnDelete" runat="server" Text="Delete"   OnClick="Button1_Click"   OnClientClick="return DeleteConfirmation();"/>
      </FooterTemplate>
  </asp:TemplateColumn>
   <asp:ButtonColumn  ButtonType="PushButton" CommandName="Update" Text="Edit"></asp:ButtonColumn>
  </Columns>
</asp:DataGrid>

JavaScript:

<script type="text/javascript" language="javascript">
function CheckAll(chk)
{
all = document.getElementsByTagName("input");
for(i=0;i<all.length;i++)
    {
      if(all[i].type=="checkbox" && all[i].id.indexOf("MyDataGrid_ct") > -1)
      {
        all[i].checked = chk.checked;
      }
    }
}
function DeleteConfirmation()
{
if (confirm("Are you sure, you want to delete selected records ?")==true)
   return true;
else
   return false;
}
</script>

.Net Code:

public void BindGrid()
{
    DataSet ds = Customer.GetCustomers();
    MyDataGrid.DataSource = ds;
    MyDataGrid.DataBind();
}
//Delete Button
protected void Button1_Click(object sender, EventArgs e)
{
    foreach (DataGridItem item in MyDataGrid.Items)
    {
        if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
        {
            if (((CheckBox)item.Cells[6].FindControl("cbRows")).Checked)
            {
                Customer.DeleteMe(Convert.ToInt32(item.Cells[0].Text));
            }
        }
    }
    BindGrid();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值