Search code examples
asp.netwebformsdatalist

why can't I get the value of the checkboxes in my datalist?


I have a group of checkboxes in a datalist populated from a query. When checked, I want those values (strings) to be added to the string from another textbox. (Basically just notes, and the checkboxes make short work of typing oft-repeated comments.)

But for some reason, I can't get the checked boxes to get recognized in my codebehind. The checkboxes render in the form, and in fiddler, I can see that some checkboxes have an "on" value, but I can't get the values into the insert string.

Right now, I just have this wrapped in the click event of the button. Do I need to do a separate databinding on the datalist? If I need to do that, how do I get the rest of the values into the click event? I tried doing the databinding separately, and then calling the databind function from the click event, but the variables I created couldn't be found in the same context.

Here's the cs code:

 foreach (DataListItem item in StatusCheckboxDataList.Items)
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
                {

                    CheckBox StatusProblemDescriptionCheckbox= (CheckBox)item.FindControl("StatusProblemDescriptionCheckbox");
                    HiddenField StatusProblemDescriptionHidden= (HiddenField)item.FindControl("StatusProblemDescriptionHidden");
                    if (StatusProblemDescriptionCheckbox != null)
                    {
                        if (StatusProblemDescriptionCheckbox.Checked == true)
                        {
                            string StatusProblemString = StatusProblemDescriptionHidden.Value;
                            StatusProblemString = "Errors Found: " + StatusProblemString + ", " + Status_notes.Text; //this is coming from the textbox... it's finding this just fine.
                            newstatusentry.s_notes = StatusProblemString;

                        }
                        else
                        {
                            string StatusProblemString = Status_notes.Text;
                            newstatusentry.s_notes = StatusProblemString;

                        }
                    }
                }
            }

It happens to be living inside an AjaxControlToolkit Accordion, but I removed the accordion, and still no go.

here's the aspx code

<asp:DataList ID="StatusCheckboxDataList" runat="server">
 <ItemTemplate>
    <asp:CheckBox ID="StatusProblemDescriptionCheckbox" runat="server" Text='<%#Eval ("statusprobDesc") %>' /><br />
     <asp:HiddenField ID="StatusProblemDescriptionHidden" runat="server" Value='<%#Eval ("statusprobDesc") %>' />
    </ItemTemplate>
</asp:DataList>

Solution

  • Be sure you are not ovewrirtting the test box values every time you loop through : Use

     newstatusentry.s_notes = newstatusentry.s_notes+ StatusProblemString; 
    

    rather Than:

     newstatusentry.s_notes = StatusProblemString