Add value attribute to ASP.NET checkbox

I programmatically add checkboxes to ASP.NET WebForm. I want to iterate through Request.Form.Keys and get the value of the flags. ASP.NET flags do not have a value attribute.

How to set the value attribute so that when iterating through Request.Form.Keys, I get a more meaningful value than the default value of "on".

Code for adding checkboxes to the page:

List<string> userApps = GetUserApplications(Context); Panel pnl = new Panel(); int index = 0; foreach (BTApplication application in Userapps) { Panel newPanel = new Panel(); CheckBox newCheckBox = new CheckBox(); newPanel.CssClass = "filterCheckbox"; newCheckBox.ID = "appSetting" + index.ToString(); newCheckBox.Text = application.Name; if (userApps.Contains(application.Name)) { newCheckBox.Checked = true; } newPanel.Controls.Add(newCheckBox); pnl.Controls.Add(newPanel); index++; } Panel appPanel = FindControlRecursive(this.FormViewAddRecordPanel, "applicationSettingsPanel") as Panel; appPanel.Controls.Add(pnl); 

Code for getting flag values ​​from Request.Form:

 StringBuilder settingsValue = new StringBuilder(); foreach (string key in Request.Form.Keys) { if (key.Contains("appSetting")) { settingsValue.Append(","); settingsValue.Append(Request.Form[key]); } } 
+8
source share
1 answer

InputAttributes.Add (!)

The following steps do not work because "the CheckBox control does not display the assigned value (it actually removes the attribute during the phase of the rendering event [)].":

 newCheckBox.Attributes.Add("Value", application.Name); 

Decision:

 newCheckBox.InputAttributes.Add("Value", application.Name); 

Thanks to Dave Parslow Blog Post: Assigning Check. ASP.Net Value

+16
source share

All Articles