开发者

How can I get the Value of a Control by iterating through its parent ControlPanel

开发者 https://www.devze.com 2023-03-15 10:27 出处:网络
I\'m dynamically populating a ControlPanel with some controls... Some are DropDowns, some are TextBoxes:

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");
 开发者_Go百科       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.


You'll have to cast item to the appropriate control type to access it's properties.

if (!(item is Label | item is LiteralControl))
{
      if(item is TextBox)
      {
        TextBox textBox = (TextBox)item;
        string textValue = textBox.Text;
      }
      ...

}


Alternatively, you can use Linq to get an IEnumerable of Textboxes and an IEnumerable of DropDownLists:

IEnumerable<TextBox> txts = Panel_Controls.Controls.OfType<TextBox>();
IEnumerable<DropDownList> ddls = Panel_Controls.Controls.OfType<DropDownList>();

The result enumerables already have the correct types. That way you can iterate through the enumerables individually, since what you do with each item is different, depending on the type.

The end result is that you will not have a buch of IF inside you loop: you will have two iteration blocks:

foreach(TextBox txt in txts)
{
    //your textbox code
}

foreach(DropDownList ddl in ddls)
{
    //your dropdownlist code
}


Can't you use the Text property of the controls? That way you won't have to care what type of control it is. What type do you need the value to be? Will string do?

foreach (Control item in Panel_Controls.Controls)
{
   string value = item.Text;
   // do something with the value
}


You should cast the item to the Textbox like:

TextBox textbox = item as TextBox;
if (textbox != null)
    string text = textbox.Text;

You can do the same for any other control

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号