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();
}
}
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>
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.
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.
Any tips or suggestions as to what I am missing would be greatly appreciated.