asp.net ListView复选框值为假,即使它已选中

s3fp2yjn  于 2023-04-22  发布在  .NET
关注(0)|答案(1)|浏览(137)

我的列表视图中有一个复选框:

<asp:ListView ID="ListViewInfoListTable" runat="server" OnPagePropertiesChanged="ListViewInfoListTable_PagePropertiesChanged" OnSorting="ListViewInfoListTable_Sorting">
    <LayoutTemplate>
        <table id="TableReceiptInfoList" runat="server" class="table table-striped table-hover table-condensed table-bordered">
            <thead>
                <tr id="HeaderRow">
                    <th><asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" Checked="false" OnCheckedChanged="chkAll_CheckedChanged"/></th>
                    <th><asp:LinkButton runat="server" CommandName="sort" CommandArgument="project">Project </asp:LinkButton></th>
                    <th><asp:LinkButton runat="server" CommandName="sort" CommandArgument="date">Date</asp:LinkButton></th>
                </tr>
            </thead>
            <tbody>
                <tr runat="server" id="itemPlaceHolder"></tr>
            </tbody>
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr id="TableRIL">
            <td><asp:CheckBox ID="chk" runat="server" AutoPostBack="false"/></td>
            <td><%# Eval("project_area").ToString() %></td>
            <td><%# Eval("date_packaged").ToString() %></td>
        </tr>
    </ItemTemplate>
</asp:ListView>

复选框事件处理程序checkAll_CheckChnaged正在切换行复选框值:

protected void chkAll_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chkAll = (CheckBox)sender;
    for (int counter =0; counter < ListViewInfoListTable.Items.Count; counter++)
    {
        if (chkAll.Checked == true)
        {
            ((CheckBox)ListViewInfoListTable.Items[counter].FindControl("chk")).Checked = true;
        }
        else
        {
            ((CheckBox)ListViewInfoListTable.Items[counter].FindControl("chk")).Checked = false;
        }
    }
}

我面临的问题是复选框值显示为false,即使在按钮单击中选中它:

protected void buttonReview_Click(object sender, EventArgs e)
{
    bool anyCheckBoxChecked = false;
    for (int counter = 0; counter < ListViewInfoListTable.Items.Count; counter++)
    {
        if (((CheckBox)ListViewInfoListTable.Items[counter].FindControl("chk")).Checked == true)
        {
            anyCheckBoxChecked = true;
        }
    }

在上面这行代码中

((CheckBox)ListViewInfoListTable.Items[counter].FindControl("chk")).

显示复选框值为假,即使复选框被选中。2请帮助。3任何帮助都是非常感谢的。

sauutmhj

sauutmhj1#

您没有显示数据绑定代码,但我认为可以安全地假设您正在调用buttonReview_Click之前重新绑定数据(这将清除所有选中的复选框)。
确保在绑定之前检查IsPostBack,例如在Page_Load事件中:

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
        return;

    BindListTable();
}

相关问题