首页 > 技术文章 > asp:DataGrid之添加asp:CheckBox做全选功能时涉及到绑值问题解决

zhuji 2016-09-01 08:49 原文

最大的意图是为asp:CheckBox的value绑定上自己需要的value值,而不是默认的字符串"on"

参考了这篇文章带Value属性的扩展CheckBox控件,意义不大,换了种解决方案

jQuery风格的全选

$(function () {
    $("#allCheck").bind("click", function () {
          $("[name = $chkItem]:checkbox").attr("checked", $(this).attr("checked"));
    });
})

 

<asp:TemplateColumn>
  <HeaderTemplate>
     <asp:CheckBox ID="allCheck" runat="server"  ClientIDMode="Static"/>
  </HeaderTemplate>
  <ItemTemplate>
      <input type="checkbox" runat="server" id="chkItem"  value='<%# DataBinder.Eval(Container.DataItem,"Id")%>' name="chkItem"/>
   </ItemTemplate>
</asp:TemplateColumn>

后台取值

string id = "";
System.Web.UI.HtmlControls.HtmlInputCheckBox chkItem;
foreach (DataGridItem oDataGridItem in this.DbList.Items)
{
    chkItem = (HtmlInputCheckBox)oDataGridItem.FindControl("chkItem");
    if (chkItem.Checked == true)
    {
        id += chkItem.Value + ";";
    }
}
if (!string.IsNullOrEmpty(id))
{
     string[] idArray = id.Split(new Char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
     ........

 

推荐阅读