C# 单击linkbutton时清除复选框列表选择(尽管回发检查)

C# 单击linkbutton时清除复选框列表选择(尽管回发检查),c#,asp.net,mono,checkboxlist,C#,Asp.net,Mono,Checkboxlist,如何在清除复选框属性之前检索它们 我想在单击“Go!”后计算复选框的数量。相反,单击后,将清除框(即使是默认为“选定”的框),并且计数为零 我是否需要另一种方法来保护这些值不被回发 → *.aspx <%@ Page Language="C#" Inherits="LunaIntraDB.sandbox" MasterPageFile="~/SiteMaster.master" %> <%@ MasterType VirtualPath="~/SiteMaster.mas

如何在清除复选框属性之前检索它们

我想在单击“Go!”后计算复选框的数量。相反,单击后,将清除框(即使是默认为“选定”的框),并且计数为零

我是否需要另一种方法来保护这些值不被回发

*.aspx

<%@ Page Language="C#" Inherits="LunaIntraDB.sandbox" MasterPageFile="~/SiteMaster.master" %>
<%@ MasterType VirtualPath="~/SiteMaster.master" %>
<asp:Content ContentPlaceHolderID="HeadContent" ID="HeadContentContent" runat="server">
</asp:Content>
<asp:Content ContentPlaceHolderID="MainContent" ID="MainContentContent" runat="server">

<asp:CheckBoxList ID="aCheckBoxList" runat="server" >
  <asp:ListItem Value="DontCheck" runat="server">1</asp:ListItem>
  <asp:ListItem Value="blah" Selected="True" runat="server">2</asp:ListItem>
</asp:CheckBoxList>

 <asp:LinkButton ID="goButton" runat="server" Text="Go!" onclick="Clicked" />

</asp:Content>

您应该在@page上尝试
EnableViewState=“True”
属性,如下所示:

<%@ Page Language="C#" Inherits="LunaIntraDB.sandbox" 
MasterPageFile="~/SiteMaster.master" 
EnableViewState="True"
%>

取自

在页面加载中(对象发送方,事件参数e):

setCheckBoxStates (aCheckBoxList);
其他地方

public static void setCheckBoxStates(CheckBoxList cbl)
        {
            // if we are postback and using mono
            if (HttpContext.Current.Request.HttpMethod == "POST"  && Type.GetType("Mono.Runtime") != null)
            {
                string cblFormID = cbl.ClientID.Replace("_","$");
                int i = 0;
                foreach (var item in cbl.Items)
                {
                    string itemSelected = HttpContext.Current.Request.Form[cblFormID + "$" + i];
                    if (itemSelected != null && itemSelected != String.Empty)
                        ((ListItem)item).Selected = true;
                    i++;
                }
            }
        }

尽管@Will的答案是正确的,但它并不是整个问题的结论

例如,当复选框列表位于中继器控件内时,ClientID具有不同的格式来指示中继器内的复选框列表位置

可以肯定地说,我们并不希望每次使用CheckBoxList时都处理这个问题,因此我创建了一个自定义控件,扩展CheckBoxList.OnLoad事件来解决上述问题

namespace YOURNAMESPACE.Controls
{
    using System.Text.RegularExpressions;
    using System;
    using System.Web;
    using WebControls = System.Web.UI.WebControls;

    public class CheckBoxList : WebControls.CheckBoxList
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (HttpContext.Current.Request.HttpMethod == "POST" && Type.GetType("Mono.Runtime") != null) {

                string cblFormID = Regex.Replace(ClientID, @"_\d+$", String.Empty).Replace("_", "$");

                int i = 0;
                foreach (WebControls.ListItem item in Items)
                    item.Selected = HttpContext.Current.Request.Form[cblFormID + "$" + i++] == item.Value;
            }
        }
    }
}

谢谢,但运气不好。。。如果我将EnableViewState设置为false,则会出现索引越界错误。(由于丢失了aspx添加的复选框,aCheckBoxList小于预期值?)System.ArgumentOutOfRangeException索引小于0或大于或等于列表计数。参数名称:索引4说明:HTTP 500。处理请求时出错。详细信息:非web异常。异常源(应用程序或对象的名称):mscorlib。“这可能是一个bug,但我把它归类为一个奇怪的东西。据我在MS规范中所知,动态复选框列表控件的复选框状态是在预渲染阶段设置的。。。虽然状态在控件中不直接可用,但它在viewstate中可用,并且可以在请求集合中访问。“但是我认为我的示例没有必要是动态的,因为asp:ListItems位于.aspx页(?)这段代码在MS.NET中可以正常工作github的mono repo中有几个pull请求,用于修复ASP.NET的复选框问题,您可以检查它们是否修复了您的错误
public static void setCheckBoxStates(CheckBoxList cbl)
        {
            // if we are postback and using mono
            if (HttpContext.Current.Request.HttpMethod == "POST"  && Type.GetType("Mono.Runtime") != null)
            {
                string cblFormID = cbl.ClientID.Replace("_","$");
                int i = 0;
                foreach (var item in cbl.Items)
                {
                    string itemSelected = HttpContext.Current.Request.Form[cblFormID + "$" + i];
                    if (itemSelected != null && itemSelected != String.Empty)
                        ((ListItem)item).Selected = true;
                    i++;
                }
            }
        }
namespace YOURNAMESPACE.Controls
{
    using System.Text.RegularExpressions;
    using System;
    using System.Web;
    using WebControls = System.Web.UI.WebControls;

    public class CheckBoxList : WebControls.CheckBoxList
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (HttpContext.Current.Request.HttpMethod == "POST" && Type.GetType("Mono.Runtime") != null) {

                string cblFormID = Regex.Replace(ClientID, @"_\d+$", String.Empty).Replace("_", "$");

                int i = 0;
                foreach (WebControls.ListItem item in Items)
                    item.Selected = HttpContext.Current.Request.Form[cblFormID + "$" + i++] == item.Value;
            }
        }
    }
}