0

I've been breaking my head on this one for a day now. But I just don't see it.

I have a checkboxlist named cblRounds which is

<asp:CheckBoxList ID="cblRondes" runat="server">
</asp:CheckBoxList>

Also to note, EnableViewstate is set to true.

In my code behind, in the page_Load i fill de list like this:

protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        dpPrintRounds.FieldValue = DateTime.Now.AddDays(1);

    }
    FillCheckBoxList(); 
}

private void FillCheckBoxList()
{
    tourCollectie = new LogisticsTourCollection();
    RelationCollection rc = new RelationCollection(LogisticsItemEntity.Relations.LogisticsItemSpecsEntityUsingSeqSpecs);
    rc.Add(LogisticsItemSpecsEntity.Relations.LocationEntityUsingSeqLocationDelivery);
    rc.Add(LocationEntity.Relations.LocationLogisticsTourEntityUsingSeqLocation);
    rc.Add(LocationLogisticsTourEntity.Relations.LogisticsTourEntityUsingSeqLogisticsTour);
    PredicateExpression pe = new PredicateExpression(LogisticsItemSpecsFields.RequiredDeliveryDate == dpPrintRounds.FieldValue);
    pe.Add(LogisticsItemFields.DeliveryNumber != DBNull.Value);
    tourCollectie.GetMulti(pe, rc);

    cblRondes.Items.Clear();
    foreach (LogisticsTourEntity tour in tourCollectie)
    {
        cblRondes.Items.Add(new ListItem(tour.Name, tour.SeqLogisticsTour.ToString()));
    }        
}

Then I click a button where I check the checkstate of the checkboxes

protected void btnPrintHeaders_Click(object sender, EventArgs e)
{
    PrintRounds();
}

private void PrintRounds()
{
    if (dpPrintRounds.Date_Selected.HasValue)
    {            
        Dictionary<string, string> rondes = new Dictionary<string, string>();
        foreach (ListItem item in cblRounds.Items)
        {
            if (item.Selected)
            {
                rondes.Add(item.Value, GetDeliveryNumberFromRonde(item.Value));
            }
        }            


    }
}

Everything works correct except that the if (item.Selected) always returns false.

Also I have

<td>
            <rm:DatePicker ID="dpPrintRounds" runat="server" />
        </td>
        <td>
            <asp:Button ID="btnSearch" runat="server" Visible="true" 
                onclick="btnSearch_Click" />
            <%--<asp:Literal ID="litLogisticsRoundName" runat="server" />:--%>
        </td>

The Datepicker returns a date I use to filter my collection. So when I press the searchbutton, I get "new" checkboxes in my checkboxlist. This is why I DON'T have the Fillcheckboxlist inside the if(!IsPostBack) else I get no checkboxes on a new search.

I've been searching for an answer on this and tried several things but none seem to work. Any ideas are appreciated.

4 Answers 4

2

Page_Load event code must be wrapped in IsPostBack block.

if(!IsPostBack)
{
  foreach (LogisticsTourEntity tour in tourCollection)
    {
        cblRounds.Items.Add(new ListItem(tour.Name, tour.SeqLogisticsTour.ToString()));
    }
}

Demo:

Markup

<form id="form1" runat="server">
    <asp:CheckBoxList 
            ID="cblRounds" 
            runat="server">
    </asp:CheckBoxList>
    <asp:Button 
            ID="Button1" 
            runat="server" 
            Text="Button" onclick="Button1_Click" />

</form>

Code-behind

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
    for (int i = 1; i <= 10; i++)
    {
        cblRounds.Items.Add(new ListItem("Text" + i ,i.ToString()));
    }
}
}
protected void Button1_Click(object sender, EventArgs e)
{
PrintRounds();
}
private void PrintRounds()
{
    Dictionary<string, string> rondes = new Dictionary<string, string>();
    foreach (ListItem item in cblRounds.Items)
    {
        if (item.Selected)
        {
            rondes.Add(item.Text , item.Value);
            Response.Write("<br/> " + item.Text + " " + item.Value);
        }
    }

}
Sign up to request clarification or add additional context in comments.

7 Comments

I did, but it doesn't change the false return.
I see why this would work. But what if I have a button that dynamically makes the checkboxes in the checkboxlist. I do not have a predefined number of checkboxes to show on the page. I make the checkboxes according to a certain "Collection" and this collection changes when i change a DatePicker field and press a "search" button. I'm sorry if I wasn't clear on this.
@Robin - Put FillCheckBoxList(); inside the IsPostBack block and what is dpPrintRounds?
It's a datapicker, seems I forgot that part but I've added it. My checkboxlist changes when I search dynamically. dpPrintRounds is a datepicker field which I use to filter on a certain date.
Is there any event of datapicker control which add postback when a new date is selected. If so then invoke FillCheckBoxList() method in that handler but Code in page_load must be within the IsPostBack block.
|
0

Have you ensured it is not due to casting to ListItem? It shouldnt be needed but worth a try...

for (int i = 0; i < cblRounds.Items.Count; i++)
{
   if (cblRounds.Items[i].Selected)
     //do your stuff
}

Also if you set a breakpoint to the place where you are dereferencing the Selected value what is it's state? Could this be an issue with at what point in the page life cycle you are binding data/user event being captured vs. where you are trying to get the selected value?

1 Comment

The binding happens before getting the selected value. And I've already put up a breakpoint there. It says that item.Selecten is false, every time.
0

Your method call FillCheckBoxList() populates the listbox on page load, whether it is a postback or not.

That means you are overriding the values posted back to the server, as your fill code always executes this:

cblRondes.Items.Clear();
foreach (LogisticsTourEntity tour in tourCollectie)
{
    cblRondes.Items.Add(new ListItem(tour.Name, tour.SeqLogisticsTour.ToString()));
}

You want to only populate the CheckBoxList's items if it it not a postback (i.e. only when the page first loads).

When it is a postback the Items collection of the CheckBoxList has already been populated by the postback itself from the contents of the webpage. You do not have to populate the control on postback.

You just want to change your page_load to:

protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        dpPrintRounds.FieldValue = DateTime.Now.AddDays(1);
        FillCheckBoxList(); 
    }
}

Comments

0

In answer to the original question, a look here may help someone:-

http://aspdotnetcodebook.blogspot.co.uk/2008/08/how-to-add-checkboxlist-dynamically.html

I have successfully dynamically created a checkboxlist, populated it with values from a db and then after changes updated the database.

2 Comments

What exactly does the link say? What would happen if this link became out dated?
@ivan - Bit of a Good question, but not wanting to copy and paste the whole blogs code really, besides reproducing someone elses work, it would fill up three or four screens of this post. Good point though whats best to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.