如何返回在asp.net CheckBoxList控件选定项目 [英] How to return selected items from checkboxlist control in asp.net
问题描述
我想在一个字符串返回从没有运气一个动态绑定的复选框列表控件中的选定项目。我希望有人可以提供帮助。在隐藏文件我的code我conencting一类称为用户,建设一个DataTable。我然后将数据表绑定到cblist中控
I am trying to return in a string the selected items from a dynamically bound checkbox list control with no luck. I'm hoping someone can help. In my code behind file I am conencting to a class called users and building a datatable. I then bind the data table to the cblist control
private void populateUserList() //called on page load
{
SubmitOptions mySubmission = new SubmitOptions(juris, rptType, tmplName);
if (mySubmission.Users.Count == 0)
{
lbl_juris.Visible = false;
cb_selectUser.Visible = false;
lbl_AlertMsg.Visible = true;
btnSelect.Visible = false;
lbl_AlertMsg.Text = "No supervisors listed for jursidiction: " + juris.ToString();
}
else
{
dt.Columns.Add("Users");
for (int i = 0; i < mySubmission.Users.Count(); i++)
{
DataRow dr = dt.NewRow();
dr["Users"] = mySubmission.Users[i];
dt.Rows.Add(dr);
}
cb_selectUser.DataSource = dt;
cb_selectUser.DataBind();
}
}
在主aspx文件我已经定义为控制:
Within the main aspx file I have the control defined as:
<asp:CheckBoxList ID="cb_selectUser"
Width="400px"
Height="100%"
AutoPostBack="false"
runat="server"
CellPadding="2"
CellSpacing="5"
DataTextField="Users"
DataValueField="Users"
>
</asp:CheckBoxList>
我曾尝试以下code,其中我在列表itterate但这似乎只工作,如果我硬code值代入Checkboxt名单listItems中。
I have tried the following code where i itterate through the list but this only seems to work if I hard code values into the Checkboxt list as listitems.
protected void btn_returnUserList(object sender, System.Web.UI.ImageClickEventArgs e)
{
for (int i = 0; i < cb_selectUser.Items.Count; i++)
{
if (cb_selectUser.Items[i].Selected)
{
selectedUsers += cb_selectUser.Items[i].Text;
}
}
该列表填充罚款和所有我想要做的就是在一个字符串从复选框列表控件中的所有选定的用户的回报。
The list populates fine and all I want to do is return in a string all selected users from the checkbox list control.
就像我说的,如果我硬code项值到控制上述code ++工程,我可以看到字符串中所选的项目但去除itemslist标签和切换到绑定nothign发生。以上方法计算回报的整个数字,但以往任何时候都返回任何选择。
As I said if I hard code the item values into the control the above code works and I can see the selected items in the string however removing the itemslist tags and switching over to a binding nothign happens. The above method counts the entire number of returns but nothing selected is ever returned.
至于是什么,我缺少的是大大AP preciated任何提示或建议。
Any tips or suggestions as to what I am missing would be greatly appreciated.
推荐答案
在这里是完全工作,只要你想它页面的完整code。只需添加的CheckBoxList形成名字它LIST1,它添加一个按钮名称BTN并添加一个标签,并将其命名为LBL。
here is complete code of page working exactly as you want it. just add a CheckboxList to form name it to list1, add a button name it to btn and add a label and name it lbl.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
var dt = new DataTable();
dt.Columns.Add("Users");
const string str = "User {0}";
for(var i=1;i<=10;i++)
{
//var r = dt.NewRow();
//r.ItemArray=new object[]{string.Format(str,i)};
dt.Rows.Add(new object[] {string.Format(str, i)});
}
list1.DataSource = dt;
list1.DataTextField = "Users";
list1.DataBind();
}
}
protected void btn_Click(object sender, EventArgs e)
{
var s = list1.Items.Cast<ListItem>()
.Where(item => item.Selected)
.Aggregate("", (current, item) => current + (item.Text + ", "));
lbl.Text = s.TrimEnd(new[] {',', ' '});
}
这篇关于如何返回在asp.net CheckBoxList控件选定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!