我正在使用一些控件动态填充 ControlPanel ...有些是 DropDowns,有些是 TextBoxes:
//inputArray is a JsonArray (thus the SelectToken methods)
foreach (var item in inputArray)
{
//Create Label
Label LabelTitle = new Label();
LabelTitle.Text = (string)item.SelectToken("title");
Panel_Controls.Controls.Add(LabelTitle);
//Create Control
if ((string)item.SelectToken("type") == "textinput")
{
TextBox TextBox_Control = new TextBox();
TextBox_Control.ID = (string)item.SelectToken("title");
Panel_Controls.Controls.Add(TextBox_Control);
}
if ((string)item.SelectToken("type") == "dropdown")
{
DropDownList DropDown_Control = new DropDownList();
DropDown_Control.DataSource = dropDownData;
DropDown_Control.DataBind();
Panel_Controls.Controls.Add(DropDown_Control);
}
}
稍后,我需要获取 DropDown 和文本框字段的值。我可以过滤掉标签和其他控件。我不知道如何在 foreach 语句中获取控件的值。我猜我需要将控件转换为可以让我获得 .Value 属性的东西,因为通用 Control 不会给我 .Value 属性。
foreach (Control item in Panel_Controls.Controls)
{
if (!(item is Label | item is LiteralControl))
{
//How can I access the .Value of the controls here?
}
}
有人可以建议一种在 foreach 循环中从 TextBox 和 DropDowns 获取值的好方法吗?
非常感谢。