I'm dynamically populating a ControlPanel with some controls... Some are DropDowns, some are 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);
}
}
Later on, I need to get the values of the DropDown and Text box fields. I can filter out the Label and other controls. I can't figure out how to get the values of the Controls within the foreach statement. I'm guessing I need to Cast the control as something that will let me get a .Value property, because the generic Control won't give me a .Value property.
foreach (Control item in Panel_Controls.Controls)
{
if (!(item is Label | item is LiteralControl))
{
//How can I access the .Value of the controls here?
}
}
Could someone suggest a good way of getting values from TextBox and DropDowns within the foreach loop?
Thanks so much.