getting dynamically created control's value as null

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • imranabdulaziz
    New Member
    • Dec 2006
    • 29

    getting dynamically created control's value as null

    Dear All,
    I am mess with one situation let me explain the scenario.
    I am making search form where I display 15 field in checkboxlist and user select one or two or three or any no to all field. Depending on his selection (he tick the field and click filter button ) Now in filter button click I create label and dropdownlist control(populat ed from database) and add it to placeholder control. Now I select value and click the search button. Now in search button I need to access those control and its selected value(ie label and droplistbox ).
    I have made function to create label and dropdownlistbox which is

    private void createcontrols( )
    {
    foreach (ListItem li in CheckBoxListmst .Items)
    {
    if (li.Selected == true)
    {

    //code added to fetch dropdownlist
    setting = ConfigurationMa nager.Connectio nStrings["StyleSearchCon nectionString"];
    if (setting != null)
    {
    Code to access data for populating dropdownlistbox
    try
    {
    // added controls
    Label label = new Label();
    DropDownList dropdownlist = new DropDownList();
    label.ID = "label" + i.ToString();
    dropdownlist.ID = "dropdownli st" + .ToString();


    i++;
    rdr = cmd.ExecuteRead er();
    while (rdr.Read())
    {
    Items for dropdownlist


    }
    PlaceHolder1.Co ntrols.Add(labe l);
    PlaceHolder1.Co ntrols.Add(drop downlist);



    }

    catch (SqlException ex)
    {

    }

    Now this create function I call in one in filter button click event and second as

    protected override void OnLoad(EventArg s e)
    {
    base.OnLoad(e);
    createcontrols( );
    }

    Now in seach button click event when I am trying to access
    Code is

    Label objlbl;
    DropDownList objdrop;
    objlbl = this.Page.FindC ontrol("label1" ) as Label;
    objdrop = this.Page.FindC ontrol("dropdow nlist1") as DropDownList;
    if ((objlbl != null) && (objdrop != null))
    {
    cmd.Parameters. Add("@para1", SqlDbType.VarCh ar).Value = objlbl.Text; //Page.FindContro l("label1").ToS tring() ;
    cmd.Parameters. Add("@value1", SqlDbType.VarCh ar).Value = objdrop.Selecte dValue;
    }
    I am getting objlbl and objdrop as null.


    Please help


    thanks
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You will need to search for them with PlaceHolder1.Fi ndControl("labe l1") since they are children of PlaceHolder1.
    I believe there is also an overload to .FindControl() that allows searching of children.

    In the following scenario (PlaceHolder1 contains the Labels and Page contains it and mytextbox):
    Page
    mytextbox
    PlaceHolder1
    Label1
    Label2
    (etc)

    Page.FindContro l() would let you find "PlaceHolde r1" and "mytextbox" , but to find the labels you would need to use the overload of FindControl() to search children's children, or do the search starting from PlaceHolder1

    Comment

    Working...