链接按钮单击时清除CheckboxList选择(尽管回发检查)

时间:2014-01-03 16:33:58

标签: c# asp.net mono checkboxlist

如何清除复选框属性?

点击" Go!"后,我想计算复选框的数量。相反,在单击后,清除框(即使是默认选择的框),计数为零。

我是否需要另一种保护回发值的方法?

Page LoadAfter "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>

* CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Data;
using System.Collections;

namespace LunaIntraDB
{

    public partial class sandbox : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                debugPrint("Load,beforeAdd");
                aCheckBoxList.Items.Add("Added 1");
                aCheckBoxList.Items.Add("Added 2");
                aCheckBoxList.Items.Add("Added 3");
                aCheckBoxList.Items[4].Selected = true;
                debugPrint("Load,addedItems");

            } else {
                debugPrint("PostBack");
            }
        }

        protected void Clicked(object sender, EventArgs e)
        {
            debugPrint("Button push");
        }

        /* Print Selected index and a count of selected checkboxes */
        protected void debugPrint(String where) {
            String count = aCheckBoxList.Items.Cast<ListItem>().Where(n => n.Selected).ToList().Count.ToString();
            System.Diagnostics.Debug.WriteLine(where +": "+ aCheckBoxList.SelectedIndex.ToString() + " =>" + count + "/"+ aCheckBoxList.Items.Count.ToString()  );
        }
    }
}

控制台输出

  

[0:]加载,之前添加:1 =&gt; 1/2

     

[0:] Load,addedItems:1 =&gt; 2/5

     

//检查一堆并点击&#34; Go!&#34;

     

[0:] PostBack:-1 =&gt; 0/5

     

[0:]按钮按下:-1 =&gt; 0/5

单声道版

Mono JIT compiler version 3.2.3 (tarball Sun Sep 22 20:38:43 UTC 2013) Copyright (C) 2002-2012 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug 
        LLVM:          supported, not enabled.
        GC:            sgen

3 个答案:

答案 0 :(得分:3)

取自http://www.strawberryfin.co.uk/blog/2012/08/22/dealing-with-dynamic-checkboxlists-losing-their-state-after-postback/

在Page_Load(对象发件人,EventArgs 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++;
                }
            }
        }

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

虽然@Will会在正确的轨道上回答,但它并不是整个问题的结论。

例如,当CheckBoxList位于Repeater控件内部时,ClientID具有不同的格式以指示Repeater内的CheckBoxList位置。

可以肯定地说,我们并不是真的想在每次使用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;
            }
        }
    }
}