gpt4 book ai didi

c# - 获取gridview复选框

转载 作者:太空宇宙 更新时间:2023-11-03 22:59:31 25 4
gpt4 key购买 nike

我有这个模板字段

        <asp:TemplateField ItemStyle-Width="40px">
<HeaderTemplate>
<asp:CheckBox ID="chkboxSelectAll" runat="server" AutoPostBack="true" OnCheckedChanged="chkboxSelectAll_CheckedChanged" />
</HeaderTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemTemplate>
<asp:CheckBox ID="chkEmp" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">

在代码隐藏中我有这段代码:

        protected void chkboxSelectAll_CheckedChanged(object sender, EventArgs e)
{
CheckBox ChkBoxHeader = (CheckBox)grdGeral.HeaderRow.FindControl("chkboxSelectAll");
foreach (GridViewRow row in grdGeral.Rows)
{
CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkEmp");
if (ChkBoxHeader.Checked == true)
{
ChkBoxRows.Checked = true;
}
else
{
ChkBoxRows.Checked = false;
}
}

}

protected void btnLista_Click(object sender, EventArgs e)
{
string strEmailTotal = "";
string strEmail = "";


foreach (GridViewRow row in grdGeral.Rows)
{
CheckBox chkBx = (CheckBox)grdGeral.FindControl("chkEmp");
if (chkBx != null)
{
if (chkBx.Checked)
{
strEmail = ((Label)grdGeral.FindControl("lblEmail")).Text;
strEmailTotal = strEmailTotal + "," + strEmail;
}
}
}

lblMail.Text = strEmailTotal ;
}

即使我在模板字段中将默认值设置为“true”,我也总是得到复选框的空值。谁能帮我这个?谢谢

最佳答案

在您的btnLista_Click 事件中,您应该使用row 而不是grdGeral:
CheckBox chkBx = (CheckBox)row.FindControl("chkEmp");
在下面也是一样的:

strEmail = ((Label)row.FindControl("lblEmail")).Text;

关于c# - 获取gridview复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43849349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com