如何获取 CheckBoxList 选定值,我所拥有的似乎不起作用 C#.NET/VisualWebPart
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9523263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the CheckBoxList selected values, what I have doesn't seem to work C#.NET/VisualWebPart
提问by anpatel
I am creating a CheckBoxList in a class file and am using an HTMLTextWriter to render the control.
我在类文件中创建一个 CheckBoxList 并使用 HTMLTextWriter 来呈现控件。
I'm using the following code to store the selected values in a string:
我正在使用以下代码将选定的值存储在字符串中:
string YrStr = "";
for (int i = 0; i < YrChkBox.Items.Count; i++)
{
if (YrChkBox.Items[i].Selected)
{
YrStr += YrChkBox.Items[i].Value + ";";
}
}
I stepped through the code and it doesn't seem to hit the inside of the if statement & the selected value attribute is false every time ... Anyone have an idea of how I can address this?
我逐步完成了代码,它似乎没有触及 if 语句的内部,并且每次选择的值属性都是 false ......有人知道我如何解决这个问题吗?
I populate it using the following:
我使用以下内容填充它:
YrChkBox.Items.Add(new ListItem("Item 1", "Item1"));
采纳答案by Walk
In your ASPX page you've got the list like this:
在您的 ASPX 页面中,您有这样的列表:
<asp:CheckBoxList ID="YrChkBox" runat="server"
onselectedindexchanged="YrChkBox_SelectedIndexChanged"></asp:CheckBoxList>
<asp:Button ID="button" runat="server" Text="Submit" />
In your code behind aspx.cs page, you have this:
在 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));
}
Ensure you use the if (!IsPostBack) { }condition because if you load it every page refresh, it's actually destroying the data.
确保您使用该if (!IsPostBack) { }条件,因为如果您在每次页面刷新时加载它,它实际上是在破坏数据。
回答by MethodMan
Try something like this:
尝试这样的事情:
foreach (ListItem listItem in YrChkBox.Items)
{
if (listItem.Selected)
{
//do some work
}
else
{
//do something else
}
}
回答by kavitha Reddy
check boxlist selected values with seperator
用分隔符复选框列出选定的值
string items = string.Empty;
foreach (ListItem i in CheckBoxList1.Items)
{
if (i.Selected == true)
{
items += i.Text + ",";
}
}
Response.Write("selected items"+ items);
回答by priya uthaya
// Page.aspx //
// 页面.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++;
}
}
// Page.aspx.cs //
// 页面.aspx.cs //
// To access checkbox list item's value //
string YrStrList = "";
foreach (ListItem listItem in ChkMonth.Items)
{
if (listItem.Selected)
{
YrStrList = YrStrList + "'" + listItem.Value + "'" + ",";
}
}
sMonthStr = YrStrList.ToString();
回答by Jomon John
// aspx.cs
// aspx.cs
// Load CheckBoxList selected items into ListBox
// 将 CheckBoxList 选中的项目加载到 ListBox
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;
}
}
}
}
回答by Fred Mauroy
An elegant way to implement this would be to make an extension method, like this:
实现这一点的一种优雅方法是创建一个扩展方法,如下所示:
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;
}
}
I can then use something like this to compose a string will all values separated by ';':
然后我可以使用这样的东西来组成一个字符串,所有的值都用 ';' 分隔:
string.Join(";", cbl.GetSelectedItems());

