Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CheckBoxList ListItem Count always 0 after adding data dynamically

I have the following code in aspx code. I wanted to add ListItem check boxes to ColumnsList and Find all the checked one's on button click.

But when I try to get the selected items on button click the ColumnsList count becomes 0.

<asp:checkboxlist runat="server" EnableViewState="true" id="ColumnsList"/>

In code behind I add data to my ColumnsList as follows

public override void OnLoad()
{
    if(!this.IsPostBack)
    {
       this.ColumnsList.Items.Add(new ListItem { Text= "Text1",  Value = "value1"    });
       this.ColumnsList.Items.Add(new ListItem { Text= "Text2",  Value = "value2"  });
    }
}

// Here is the button click listener

private void Button_Click(object sender, EventArgs eventArgs)
{
    // Count is 0 instead of 2
    var count = this.ColumnsList.Items.Count;
    foreach(ListItem item in this.ColumnsList.Items)
    {
        var selected = item.Selected;
        // add selected to a list..etc

    }
}           

Note: The application is Deployed in share point 2010.

like image 933
Helen Araya Avatar asked Sep 24 '15 22:09

Helen Araya


4 Answers

I tried to simulate what you were trying and here is the solution step by step.

Step 1: Instead of creating override OnLoad() method, you can use Page_Load() method to add items to your ComboBoxList control, like below. Don't forget to put a comma between Text and Value property while creating a new ListItem.

protected void Page_Load(object sender, EventArgs e)
{
    if(!this.IsPostBack)
    {
        this.ColumnsList.Items.Add(new ListItem { Text= "Text1",  Value = "value1" });
        this.ColumnsList.Items.Add(new ListItem { Text = "Text2", Value = "value2" }); 
        this.ColumnsList.Items.Add(new ListItem { Text = "Text3", Value = "value3" });
        this.ColumnsList.Items.Add(new ListItem { Text = "Text4", Value = "value4" });
    }
}

Step 2: After this, I created a button click event like yours, but wrote only single line there to get the count of selected items as shown below.

protected void Button1_Click(object sender, EventArgs e)
{
    var count = this.ColumnsList.Items.Cast<ListItem>().Count(li => li.Selected);
}

Note: Check your button click event. this.ColumnsList.Items.Count will return you count of the items that were there in the ComboBoxList and item.Selected from loop will tell you whether the item was selected or not. However var selected will give you the status of last item, as you are overriding its value for each item.

like image 69
Amnesh Goel Avatar answered Oct 31 '22 23:10

Amnesh Goel


I ended up moving the code which loads the List from pageload to OnInit as follows and it worked.

protected override void OnInit(EventArgs e)
{
  this.ColumnsList.Items.Add(new ListItem { Text= "Text1",  Value = "value1"    });
  this.ColumnsList.Items.Add(new ListItem { Text= "Text2",  Value = "value2"  });

}

like image 40
Helen Araya Avatar answered Nov 01 '22 00:11

Helen Araya


Your code works fine in my solution except for the following:

The implementation of your OnLoad does not have the override parameter EventArgs. In my solution, its a requirement to declare the EventArgs parameter in OnLoad().

protected override void OnLoad(EventArgs e)
{
   //base.OnLoad(e);
   if(!this.IsPostBack)
   {
       this.ColumnsList.Items.Add(new ListItem { Text= "Text1", Value = "value1"    });
       this.ColumnsList.Items.Add(new ListItem { Text= "Text2", Value = "value2"  });
   }
}

And lastly, is the comma separator in Text='text' Value='value' to Text='text', Value='value' Anything else works fine.

EDIT : here's my button implementation.

protected void Button_Click(object sender, EventArgs e)
{
    foreach (ListItem itemList in ColumnsList.Items)
    {
       if (itemList.Selected)
       {
          // selected
       }
    }
}
like image 2
ken lacoste Avatar answered Oct 31 '22 22:10

ken lacoste


You should add dynamic data on PreInit evet when you need to reate or re-create dynamic controls:

protected override void OnPreInit(EventArgs e)
{
    if(!this.IsPostBack)
    {
       this.ColumnsList.Items.Add(new ListItem { Text= "Text1", Value = "value1" });
       this.ColumnsList.Items.Add(new ListItem { Text= "Text2", Value = "value2" });
    }
}

More information about page life cycle.

like image 2
Backs Avatar answered Oct 31 '22 22:10

Backs