2012-01-18 78 views
2

我有GridView的标题和项目复选框。当我选择标题复选框我的代码能够选择所有项目复选框。当我取消选择标题复选框我的代码能够联合选择所有项目复选框。在asp.net的gridview默认选择标题和项目复选框

这里我的问题是当我的网页加载(意味着当网格加载第一次)默认情况下,应选择标题和项目复选框,我们可以做到这一点。

回答

0

使用RowDataBound事件中的GridView

protected void GridViewName_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row .RowType == DataControlRowType .DataRow) 
     { 
      //Your Code 
     } 
    } 
0

在网格视图的列写这样

<Columns> 
           <asp:TemplateField> 
            <HeaderTemplate> 
             <asp:CheckBox ID="chkhead" runat="server" onclick="SelectAllCheckboxes(this)" ClientIDMode="Static" /> 
            </HeaderTemplate> 
            <ItemTemplate> 
             <asp:CheckBox ID="chksub" runat="server" ClientIDMode="Static" /> 
            </ItemTemplate> 
           </asp:TemplateField> 

是编写Java脚本函数SelectAllCheckboxes后写

// select all check boxes 

function SelectAllCheckboxes(spanChk) { 
    // Added as ASPX uses SPAN for checkbox 
    var oItem = spanChk.children; 
    var theBox = (spanChk.type == "checkbox") ? 
     spanChk : spanChk.children.item[0]; 
    xState = theBox.checked; 
    elm = theBox.form.elements; 
    for (i = 0; i < elm.length; i++) 
     if (elm[i].type == "checkbox" && elm[i].id != theBox.id) { 
      //elm[i].click(); 
      if (!elm[i].disabled) { 
       if (elm[i].checked != xState) 
        elm[i].click(); 
      } 
      //elm[i].checked=xState; 
     } 
    } 

后.cs页面loa d事件首先绑定网格视图和chheader.checked = true;