문제

I have a panel that holds some components of asp.net.I am generating this components(like dropdownlist, checkbox,textbox etc.) according to my data.

Example for dropdown :

System.Web.UI.WebControls.Panel comboBoxOlustur(ANKETQUESTIONBec bec)
{
    System.Web.UI.WebControls.Panel p = new System.Web.UI.WebControls.Panel();

    Table tb = new Table();
    TableRow tr = new TableRow();
    TableCell tdSoru = new TableCell(), tdComboBox = new TableCell();
    System.Web.UI.WebControls.DropDownList cmb = new System.Web.UI.WebControls.DropDownList();

    tdSoru.Text = bec.QUESTION;
    tdSoru.Width = 350;
    tdSoru.VerticalAlign = VerticalAlign.Top;

    if (bec.WIDTH != null)
        cmb.Width = (short)bec.WIDTH;
    else
        cmb.Width = 200;
    cmb.Height = 18;
    //data operations
    QUESTIONSELECTIONDEFINITIONBlc blc = new QUESTIONSELECTIONDEFINITIONBlc();
    List<QUESTIONSELECTIONDEFINITIONBec> secenekler = blc.GetByAnketQueID(bec.ID,1);

    if (secenekler != null)
    {
        ListItem li;
        li = new ListItem();
        li.Value = "-1";
        li.Text = "--Seçiniz--";
        cmb.Items.Add(li);
        for (int i = 0; i < secenekler.Count; i++)
        {
            li = new ListItem();
            li.Value = secenekler[i].ID.ToString();
            li.Text = secenekler[i].NAME;

            cmb.Items.Add(li);
        }
    }
    //end of data operations
    tdComboBox.Controls.Add(cmb);
    tr.Cells.Add(tdSoru);
    tr.Cells.Add(tdComboBox);
    tb.Rows.Add(tr);
    p.Controls.Add(tb);

    return p;
}

In this point i want to reach to this dropdownlist to get value of it.How can i implement this?

도움이 되었습니까?

해결책

I suspect the best way is to appropriately name your controls and then use FindControl.

You will probably need to use FindControl recursively in order to easily search down multiple layers.

Depending on your needs, it may also make sense to declare a variable, or variable array, that tracks each of the controls added. It's possible that this approach could be used in a way that eliminates the need to search for the controls and would therefore be more efficient.

다른 팁

I have used something like this to get all the child controls:

    private void GetAllControls(Control container, Type type)
    {
        foreach (Control control in container.Controls)
        {
            if (control.Controls.Count > 0)
            {
                GetAllControls(control, type);
            }

            if (control.GetType() == type) ControlList.Add(control);
        }
    }

and then do something like:

    this.GetAllControls(this.YourPanel, typeof(Button));
    this.GetAllControls(this.YourPanel, typeof(DropDownList));
    this.GetAllControls(this.YourPanel, typeof(TextBox));
    this.GetAllControls(this.YourPanel, typeof(CheckBox));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top