8
我以编程方式将复选框添加到ASP.NET WebForm。我想遍历Request.Form.Keys并获取复选框的值。 ASP.NET复选框没有值属性。将值属性添加到ASP.NET复选框
如何设置value属性,以便在遍历Request.Form.Keys时获得比默认“on”更有意义的值。
代码添加复选框的页面:
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);
代码从的Request.Form检索复选框值:
StringBuilder settingsValue = new StringBuilder();
foreach (string key in Request.Form.Keys)
{
if (key.Contains("appSetting"))
{
settingsValue.Append(",");
settingsValue.Append(Request.Form[key]);
}
}