简体   繁体   中英

Dynamic Checkbox.Checked if statement

Below is my code, the code creates a new CheckBox for each HyperLink, it then names each CheckBox with the name of the Hyperlink. I then have an IF statement which checks whether the dynamically created CheckBox has been checked. If it has, display the name of the CheckBox in a label.

However.. the label is not populating. I have tested everything and the label gets populated if i do it manually. The label has saved view state aswell. Any ideas? :)

foreach (HyperLink folder in folderList)
            {
                CheckBox checkbox = new CheckBox();
                checkbox.Text = folder.Text.ToString();
                folder.Controls.Add(check);
                checkbox.EnableViewState = true;
                checkbox.AutoPostBack = true;    

                if(check.Checked)
                {
                    selectedFolder.Text = check.Text.ToString();
                }

            }

It appears to me that you are not leaving any time for the checkbox to be checked (user input). You should use an event handler or something similar to capture the event that a checkbox is checked.

Also, I don't see "check" being defined anywhere.. Do you mean "checkbox" instead of "check"?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM