2012-01-18 79 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;