我正在使用我在 aspx 文件中的 gridview 执行此操作
<asp:GridView ID="gridDepartement" runat="server" CellPadding="4" ForeColor="Black"
GridLines="Horizontal" AutoGenerateColumns="False" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" AllowSorting="True" >
<Columns>
<asp:templatefield>
<HeaderTemplate>
<asp:CheckBox ID="cbSelectAll"
runat="server" AutoPostBack="true"
OnCheckedChanged="cbSelectAll_CheckedChanged" />
</HeaderTemplate>
<itemtemplate>
<asp:CheckBox Id="cbSelectOne" runat="server"/>
</itemtemplate>
</asp:templatefield>
<asp:CommandField ShowEditButton="True" ItemStyle-Width="20"/>
<asp:CommandField ShowDeleteButton="True" ItemStyle-Width="20"/>
<asp:CommandField ShowSelectButton="True" ItemStyle-Width="20"/>
<asp:boundfield headertext="Departement Code" datafield="departementcode"
ItemStyle-HorizontalAlign="Center"/>
<asp:boundfield headertext="Departement Name" datafield="departementname"
ItemStyle-HorizontalAlign="Center" />
<asp:boundfield headertext="Created By" datafield="createby"
ItemStyle-HorizontalAlign="Center" />
<asp:boundfield headertext="Created Date" datafield="createdate"
ItemStyle-HorizontalAlign="Center" />
<asp:boundfield headertext="Updated By Name" datafield="updateby"
ItemStyle-HorizontalAlign="Center" />
<asp:boundfield headertext="Last Update" datafield="lastupdate"
ItemStyle-HorizontalAlign="Center" />
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />
</asp:GridView>
我想在单击复选框标题时检查所有复选框,因此我将一个事件添加到我的网格中,并将此代码添加到复选框 checkchanged 事件中:
protected void cbSelectAll_CheckedChanged(object sender, EventArgs e)
{
bool chkFlag = false;
CheckBox cbHD = (CheckBox)gridDepartement.HeaderRow.FindControl("cbSelectAll");
if (cbHD.Checked)
{
chkFlag = true;
}
foreach (GridViewRow dr in gridDepartement.Rows)
{
CheckBox chk = (CheckBox)dr.Cells[0].FindControl("cbSelectOne");
chk.Checked = chkFlag;
}
}
页面加载代码:
protected void Page_Load(object sender, EventArgs e)
{
//if(!IsPostBack)
//{
DataSourceDepartement dpt = new DataSourceDepartement();
DataSourceDepartementTableAdapters.departementTableAdapter
adp = new DataSourceDepartementTableAdapters.departementTableAdapter();
//bind gridview to datatable
gridDepartement.DataSource = adp.GetDataDepartement();
gridDepartement.DataBind();
//}
}
好的,它现在可以工作了,但是现在的问题是,我的复选框事件仅在值变为选中/true时触发,但是当我取消选中它/将其变为false时,它没有触发,我应该修复什么部分?