DataGrid Header Checkbox need way to set after postback
I have a Header CheckBox in a DataGrid on an aspx page and it is used to select all Columns in the DataGrid, DataGrid Items (incliding Checkbox) are bound to SQL data and gets updated when header checkbox is selected via code behind. Problem is after re binding datasource to reflect changed to SQL data, the Header CheckBox is alays unCheck after DataBinding and postback.
Is there a way to toggle the Header CheckBox in code Behind
Data Grid
<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>
I recommend you call the OnClick method of mainCB button after you have done databinding and posted back.
精彩评论