dwarf
dwarf

Reputation: 455

Get Textbox by id without looping. i have the id in a variable

so i got this:

       foreach (DropDownList drp in pnl.Controls.OfType<DropDownList>())
    {
        if (drp.SelectedIndex > 0)
        {
            string textboxMinValue = "minval";
            foreach (TextBox txt in pnl.Controls.OfType<TextBox>())
            {
                if (txt.ID == "txtStatMin" + drp.ID.Substring(6,1))
                {
                    if (txt.Text != "min")
                    {
                        textboxMinValue = txt.Text;
                        statMin.Add(textboxMinValue);
                    }
                    else
                    {
                        statMin.Add(textboxMinValue);
                    }
                }
            }
            statName.Add(drp.SelectedValue);
        }
    }

i have a panel with drop downs and text boxes. i loop through the drop downs, if something was selected, i need the value in a text box. drop down id's are ddStat0 ddStat1 etc textboxes are txtStatMin0 txtStatMin1 etc

i want to remove the foreach loop of the text boxes, no reason to go through them all, i know which one i want. i cant grasp it though.

Upvotes: 0

Views: 883

Answers (1)

Xaqron
Xaqron

Reputation: 30847

Control c = FindControl("Control_ID");
// Check for null and cast it:
TextBox t = (TextBox) c;

Upvotes: 1

Related Questions