problem with accessing control created dynamically

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

    problem with accessing control created dynamically

    Dear All,

    I am using asp.net2.0, C#, sql2005 using Visual studio 2005

    Let Me explain the scenario I have checkboxlist containg 15 field. Based on no of checked field . I created dropdownlistbox and label dynamically through loop and assign id as dropdownlist + no of iteration. Now my question is how I access these control as when I am accessing one of the control I am getting nullobjectrefer ence error stating “Object reference not set to an instance of an object.”
    I created control as

    Foreach(listite m)
    {

    //…….
    Label label = new Label();
    DropDownList dropdownlist = new DropDownList();
    //assinging id
    label.ID = "label" + i.ToString() ;
    dropdownlist.ID = "dropdownli st" + i.ToString() ;

    }
    And I am accessing it through

    cmd.Parameters. Add("@para1", SqlDbType.VarCh ar).Value = Page.FindContro l("label1").ToS tring() ;
    cmd.Parameters. Add("@value1", SqlDbType.VarCh ar).Value = Page.FindContro l("dropdownlist 1").ToString ();

    and my second question is how do i access selected value in dropdownlist1(d ynamically created control) Please guide me

    Thanks
  • krris
    New Member
    • May 2007
    • 10

    #2
    Originally posted by imranabdulaziz
    Dear All,

    I am using asp.net2.0, C#, sql2005 using Visual studio 2005

    Let Me explain the scenario I have checkboxlist containg 15 field. Based on no of checked field . I created dropdownlistbox and label dynamically through loop and assign id as dropdownlist + no of iteration. Now my question is how I access these control as when I am accessing one of the control I am getting nullobjectrefer ence error stating “Object reference not set to an instance of an object.”
    I created control as

    Foreach(listite m)
    {

    //…….
    Label label = new Label();
    DropDownList dropdownlist = new DropDownList();
    //assinging id
    label.ID = "label" + i.ToString() ;
    dropdownlist.ID = "dropdownli st" + i.ToString() ;

    }
    And I am accessing it through

    cmd.Parameters. Add("@para1", SqlDbType.VarCh ar).Value = Page.FindContro l("label1").ToS tring() ;
    cmd.Parameters. Add("@value1", SqlDbType.VarCh ar).Value = Page.FindContro l("dropdownlist 1").ToString ();

    and my second question is how do i access selected value in dropdownlist1(d ynamically created control) Please guide me

    Thanks

    Hi
    u r declare the label and Drop down in loop so when loop is over object scop is also over. if u declare label and dropdown out of loop with array then it will help u.

    Comment

    • hellboss
      New Member
      • Jun 2007
      • 50

      #3
      Hello sir ! can u provide me with the code for creating a text box control array when a button is clicked ?

      Comment

      • TRScheel
        Recognized Expert Contributor
        • Apr 2007
        • 638

        #4
        Originally posted by hellboss
        Hello sir ! can u provide me with the code for creating a text box control array when a button is clicked ?
        What solutions have you tried? Have you considered a hidden frame that is visible once the button is clicked, then disposed of when no longer needed?

        Comment

        • TRScheel
          Recognized Expert Contributor
          • Apr 2007
          • 638

          #5
          Originally posted by imranabdulaziz
          my second question is how do i access selected value in dropdownlist1(d ynamically created control) [/B] Please guide me

          Thanks

          You need to cast the found control to the proper type.

          [CODE=cpp]((DropDownList) Page.FindContro l("MyDropDownLi st"))[/CODE]

          Comment

          Working...