Hairgami_Master
Hairgami_Master

Reputation: 5539

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

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.

Upvotes: 1

Views: 1227

Answers (4)

Peter
Peter

Reputation: 27944

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

Upvotes: 0

Philip Wallace
Philip Wallace

Reputation: 8015

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
}

Upvotes: 0

Adriano Carneiro
Adriano Carneiro

Reputation: 58595

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
}

Upvotes: 1

Bala R
Bala R

Reputation: 108947

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;
      }
      ...

}

Upvotes: 2

Related Questions