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.
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 usePage_Load()
method to add items to yourComboBoxList
control, like below. Don't forget to put acomma
betweenText
andValue
property while creating a newListItem
.
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.
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" });
}
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
}
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With