有什么办法只在 CheckBoxList 中获取选定的索引吗?
我只想在 checkBox lsit 中获取选定的索引或项目,而不是像 ListBox 中的 Like 那样迭代每个项目。
我在两种情况下获取选定的值通过这种方式:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class ChkBxList_2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string li = "";
foreach(ListItem lt in CheckBoxList1.Items)
{
if(lt.Selected)
li += lt.Text;
}
Response.Write(li);
}
protected void Button2_Click(object sender, EventArgs e)
{
string li = "";
foreach (int lt in ListBox1.GetSelectedIndices())
{
li += ListBox1.Items[lt].Text;
}
Response.Write(li);
}
}
在列表框中,我们可以选择仅获取查看的项目是否有复选框列表?
I want to get the selected Indices or Items only in checkBox lsit instead of iterating through each item as Like is there in ListBox.
I am Getting Selected Value In tWo case In this way:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class ChkBxList_2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string li = "";
foreach(ListItem lt in CheckBoxList1.Items)
{
if(lt.Selected)
li += lt.Text;
}
Response.Write(li);
}
protected void Button2_Click(object sender, EventArgs e)
{
string li = "";
foreach (int lt in ListBox1.GetSelectedIndices())
{
li += ListBox1.Items[lt].Text;
}
Response.Write(li);
}
}
In ListBox we have the Option To get Seected Only Items is there any For Check Box List?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为不存在,但您可以使用这个扩展方法来实现这一点:
I dont think there is but you could use this extension method that does exactly that:
你实际上回答了你自己的问题。与
ListBox
控件不同,无法获取CheckBoxList
控件中仅选定的项目。本文有一个解释和解决方法帮助。
http://weblogs.asp.net/jgalloway/archive/ 2005/10/02/426346.aspx
You actually answered your own question. There is no way to get the selected only items in the
CheckBoxList
control, unlike theListBox
control.This article has an explanation and a work around help method.
http://weblogs.asp.net/jgalloway/archive/2005/10/02/426346.aspx