C#-CheckboxList选择清除链接按钮单击(尽管回发检查)
作者:互联网
清除复选框属性之前如何检索它们?
单击“开始!”后,我想计算复选框的数量.相反,单击后,将清除这些框(甚至是默认为选中的框),并且计数为零.
我是否需要另一种保护值免受回发的方法?
→
* .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:] Load,beforeAdd: 1 =>1/2
[0:] Load,addedItems: 1 =>2/5
// check a bunch and click “Go!”
[0:] PostBack: -1 =>0/5
[0:] Button push: -1 =>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
解决方法:
尽管@Will的答案是正确的,但这并不是整个问题的结论.
例如,当CheckBoxList位于Repeater控件内部时,ClientID具有不同的格式以指示CheckBoxList在Repeater中的位置.
可以肯定地说,我们并不是真的希望每次使用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 onl oad(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;
}
}
}
}
标签:checkboxlist,mono,asp-net,c 来源: https://codeday.me/bug/20191122/2058627.html