如何获取复选框列表中的选定值,我所拥有的并不是';C#NET/VisualWebPart似乎不起作用
我正在类文件中创建一个复选框列表,并使用HTMLTextWriter呈现控件如何获取复选框列表中的选定值,我所拥有的并不是';C#NET/VisualWebPart似乎不起作用,c#,asp.net,html,htmltextwriter,C#,Asp.net,Html,Htmltextwriter,我正在类文件中创建一个复选框列表,并使用HTMLTextWriter呈现控件 // To count checklist item int a = ChkMonth.Items.Count; int count = 0; for (var i = 0; i < a; i++) { if (ChkMonth.Items[i].Selected == true) {
// To count checklist item
int a = ChkMonth.Items.Count;
int count = 0;
for (var i = 0; i < a; i++)
{
if (ChkMonth.Items[i].Selected == true)
{
count++;
}
}
我使用以下代码将所选值存储在字符串中:
string YrStr = "";
for (int i = 0; i < YrChkBox.Items.Count; i++)
{
if (YrChkBox.Items[i].Selected)
{
YrStr += YrChkBox.Items[i].Value + ";";
}
}
// To count checklist item
int a = ChkMonth.Items.Count;
int count = 0;
for (var i = 0; i < a; i++)
{
if (ChkMonth.Items[i].Selected == true)
{
count++;
}
}
试着这样做:
foreach (ListItem listItem in YrChkBox.Items)
{
if (listItem.Selected)
{
//do some work
}
else
{
//do something else
}
}
<asp:CheckBoxList ID="YrChkBox" runat="server"
onselectedindexchanged="YrChkBox_SelectedIndexChanged"></asp:CheckBoxList>
<asp:Button ID="button" runat="server" Text="Submit" />
// To count checklist item
int a = ChkMonth.Items.Count;
int count = 0;
for (var i = 0; i < a; i++)
{
if (ChkMonth.Items[i].Selected == true)
{
count++;
}
}
public static class Extensions
{
public static List<string> GetSelectedItems(this CheckBoxList cbl)
{
var result = new List<string>();
foreach (ListItem item in cbl.Items)
if (item.Selected)
result.Add(item.Value);
return result;
}
}
在您的ASPX页面中,您可以看到如下列表:
foreach (ListItem listItem in YrChkBox.Items)
{
if (listItem.Selected)
{
//do some work
}
else
{
//do something else
}
}
<asp:CheckBoxList ID="YrChkBox" runat="server"
onselectedindexchanged="YrChkBox_SelectedIndexChanged"></asp:CheckBoxList>
<asp:Button ID="button" runat="server" Text="Submit" />
// To count checklist item
int a = ChkMonth.Items.Count;
int count = 0;
for (var i = 0; i < a; i++)
{
if (ChkMonth.Items[i].Selected == true)
{
count++;
}
}
public static class Extensions
{
public static List<string> GetSelectedItems(this CheckBoxList cbl)
{
var result = new List<string>();
foreach (ListItem item in cbl.Items)
if (item.Selected)
result.Add(item.Value);
return result;
}
}
在您的代码隐藏aspx.cs页面中,您有以下内容:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Populate the CheckBoxList items only when it's not a postback.
YrChkBox.Items.Add(new ListItem("Item 1", "Item1"));
YrChkBox.Items.Add(new ListItem("Item 2", "Item2"));
}
}
protected void YrChkBox_SelectedIndexChanged(object sender, EventArgs e)
{
// Create the list to store.
List<String> YrStrList = new List<string>();
// Loop through each item.
foreach (ListItem item in YrChkBox.Items)
{
if (item.Selected)
{
// If the item is selected, add the value to the list.
YrStrList.Add(item.Value);
}
else
{
// Item is not selected, do something else.
}
}
// Join the string together using the ; delimiter.
String YrStr = String.Join(";", YrStrList.ToArray());
// Write to the page the value.
Response.Write(String.Concat("Selected Items: ", YrStr));
}
// To count checklist item
int a = ChkMonth.Items.Count;
int count = 0;
for (var i = 0; i < a; i++)
{
if (ChkMonth.Items[i].Selected == true)
{
count++;
}
}
受保护的无效页面加载(对象发送方,事件参数e)
{
如果(!IsPostBack)
{
//仅当不是回发时才填充复选框列表项。
YrChkBox.Items.Add(新列表项(“项1”、“项1”);
YrChkBox.Items.Add(新列表项(“第2项”、“第2项”);
}
}
受保护的无效YrChkBox\u SelectedIndexChanged(对象发送方,事件参数e)
{
//创建要存储的列表。
List YrStrList=新列表();
//循环浏览每个项目。
foreach(YrChkBox.Items中的ListItem项)
{
如果(选定项)
{
//如果选择了该项,请将该值添加到列表中。
YrStrList.Add(项值);
}
其他的
{
//未选择项目,请执行其他操作。
}
}
//使用;分隔符将字符串连接在一起。
String YrStr=String.Join(“;”,YrStrList.ToArray());
//将值写入页面。
Write(String.Concat(“选定项:”,YrStr));
}
确保使用if(!IsPostBack){}
条件,因为如果在每次页面刷新时加载它,它实际上会破坏数据。用分隔符选中框列表所选值
// To count checklist item
int a = ChkMonth.Items.Count;
int count = 0;
for (var i = 0; i < a; i++)
{
if (ChkMonth.Items[i].Selected == true)
{
count++;
}
}
//Page.aspx/// To count checklist item
int a = ChkMonth.Items.Count;
int count = 0;
for (var i = 0; i < a; i++)
{
if (ChkMonth.Items[i].Selected == true)
{
count++;
}
}
//aspx.cs
// To count checklist item
int a = ChkMonth.Items.Count;
int count = 0;
for (var i = 0; i < a; i++)
{
if (ChkMonth.Items[i].Selected == true)
{
count++;
}
}
//将选中的项目加载到列表框中
// To count checklist item
int a = ChkMonth.Items.Count;
int count = 0;
for (var i = 0; i < a; i++)
{
if (ChkMonth.Items[i].Selected == true)
{
count++;
}
}
int status = 1;
foreach (ListItem s in chklstStates.Items )
{
if (s.Selected == true)
{
if (ListBox1.Items.Count == 0)
{
ListBox1.Items.Add(s.Text);
}
else
{
foreach (ListItem list in ListBox1.Items)
{
if (list.Text == s.Text)
{
status = status * 0;
}
else
{
status = status * 1;
}
}
if (status == 0)
{ }
else
{
ListBox1.Items.Add(s.Text);
}
status = 1;
}
}
}
}
实现这一点的一种优雅方法是创建一个扩展方法,如下所示:
foreach (ListItem listItem in YrChkBox.Items)
{
if (listItem.Selected)
{
//do some work
}
else
{
//do something else
}
}
<asp:CheckBoxList ID="YrChkBox" runat="server"
onselectedindexchanged="YrChkBox_SelectedIndexChanged"></asp:CheckBoxList>
<asp:Button ID="button" runat="server" Text="Submit" />
// To count checklist item
int a = ChkMonth.Items.Count;
int count = 0;
for (var i = 0; i < a; i++)
{
if (ChkMonth.Items[i].Selected == true)
{
count++;
}
}
public static class Extensions
{
public static List<string> GetSelectedItems(this CheckBoxList cbl)
{
var result = new List<string>();
foreach (ListItem item in cbl.Items)
if (item.Selected)
result.Add(item.Value);
return result;
}
}
这段代码应该有效。。您将此代码放置在什么事件下?您是否实际使用YrChkBox.Items.Count有一个值..?OnClick;if语句的答案似乎总是错误的。这是在一个类文件中,回发与此有关吗?我在标记中根本没有它。我把它放在一个文字书写器里。给我一点时间,我将粘贴该部分的代码。啊,我明白你的意思,在选定值时将其添加到列表中。。。我会尝试一下,我担心每次用户选择复选框时都会重新加载页面,虽然您可以从ASPX页面中删除AutoPostBack=“True”部分,但在另一个事件(如按下按钮)的回发事件中,它的行为应该与此相同。这只是一个例子。我似乎无法在代码隐藏中添加服务器端控件,并通过HTMLTextWriter添加渲染。。。有没有关于我如何做到这一点的想法。我真的需要能够检索复选框列表的选定值,感谢您的帮助+1@Kraze正是我现在所拥有的。。除了我有一个for循环,我尝试了你的语法,是的,事实上是一样的,但我更喜欢你的语法,所以我会保持lol+1Ahh,我明白你的意思,是的,你是对的,我没有这么做。我真的希望这能有所帮助,尽管我也有同样的问题。。我想这是因为我的按钮不是服务器端的