vendredi 29 janvier 2016

How to deny dynamic name generation when creating a checkbox list dynamically

Im creating a checkbox list with HtmlInputCheckBox based on some database entries.

Due to validation purposes it's required that all the checkboxes has the same name.

Unfortunately the name attributes are always dynamically generated.

Any suggestions?

Code:

        foreach (var item in tableItems)
        {
            index += 1;              
            var value = item.GetStringValue("Text", string.Empty);

            var checkbox = new HtmlInputCheckBox();
            checkbox.Name = this.ID;  //<-- This has to be the same for all checkboxes                
            checkbox.Attributes.Add("class", CheckBoxCssClasses);
            checkbox.ID = this.ID + index;
            checkbox.Attributes.Add("value", value);
            if (Required == true)
                checkbox.Attributes.Add("required", "required");
            dynamicCheckboxes.Controls.Add(checkbox);

            var label = new HtmlGenericControl("label");
            label.Attributes.Add("class", LabelCssClasses);
            label.Attributes.Add("for", this.ID + index);
            label.InnerHtml = value;
            dynamicCheckboxes.Controls.Add(label);                
        }




Aucun commentaire:

Enregistrer un commentaire