DataGrid Header Checkbox需要在回发后设置

时间:2010-11-26 15:00:17

标签: c# asp.net

我在aspx页面上的DataGrid中有一个Header CheckBox,它用于选择DataGrid中的所有列,DataGrid Items(包括Checkbox)绑定到SQL数据,并在通过代码后面选中header复选框时更新。问题是在重新绑定数据源以反映更改为SQL数据之后,Header CheckBox在DataBinding和postback之后是unCheck。

有没有办法在代码背后切换标题CheckBox

数据网格

     <asp:DataGrid ID="Grid" runat="server" PageSize="5" AllowPaging="True" 
       DataKeyField="ID" AutoGenerateColumns="False" CellPadding="4" 
       ForeColor="#333333" GridLines="None" OnPageIndexChanged="Grid_PageIndexChanged"
       OnCancelCommand="Grid_CancelCommand" OnDeleteCommand="Grid_DeleteCommand" 
       OnEditCommand="Grid_EditCommand" OnUpdateCommand="Grid_UpdateCommand"> 
   <Columns> 
       <asp:BoundColumn HeaderText="" DataField="ID" visible="False" > </asp:BoundColumn> 
       <asp:BoundColumn HeaderText="Process Name" DataField="ProcessName"> </asp:BoundColumn> 
       <asp:BoundColumn HeaderText="Step" DataField="Order">  </asp:BoundColumn> 
       <asp:BoundColumn HeaderText="Instructions" DataField="Instructions"> </asp:BoundColumn> 
       <asp:TemplateColumn>
           <ItemTemplate>
               <asp:CheckBox ID="cbRows" runat="server"  Checked='<%# DataBinder.Eval(Container.DataItem, "Completed") %>' AutoPostBack="true"  OnCheckedChanged= "Grid_Updatecheckbox" />
           </ItemTemplate>
           <HeaderTemplate >
           <%--<asp:Button runat="server" ID="mainCB" onclick="CheckAll" Text="Chk"/>--%>
           <%--<input type="checkbox" id="mainCB"  runat = "server"  onclick="javascript:CheckAll(this);"  />--%>
           <asp:Checkbox id="mainCB"  AutoPostBack = "true"   runat = "server" OnCheckedChanged="CheckAll"   />
           </HeaderTemplate>
           </asp:TemplateColumn>
       <asp:BoundColumn HeaderText="Completed" DataField="Completed"> </asp:BoundColumn> 
       <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" HeaderText="Edit"> </asp:EditCommandColumn> 
       <asp:ButtonColumn CommandName="Delete" HeaderText="Delete" Text="Delete"> </asp:ButtonColumn> 
   </Columns> 
       <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> 
       <SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /> 
       <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center"  Mode="NumericPages" /> 
       <AlternatingItemStyle BackColor="White" /> 
       <ItemStyle BackColor="#FFFBD6" ForeColor="#333333" /> 
       <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> 
   </asp:DataGrid> 

1 个答案:

答案 0 :(得分:0)

我建议您在完成数据绑定并回发后调用mainCB按钮的OnClick方法。