Dynamically Load DropDownList Into Page

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • navyjax2
    New Member
    • Aug 2007
    • 18

    #1

    Dynamically Load DropDownList Into Page

    Not enough info on the user control part. You only have how to load the user control itself, dynamically, from the host page, which only in rare circumstances would you need to do (you could always redirect a user to another page, instead). You don't say how one might load something within that user control dynamically, how to get handles to the form, etc. In my user control's code-behind I put
    Code:
    public HtmlForm aspnetForm;
    and tried
    Code:
    aspnetForm.Controls.Add(myDropDown);
    after creating "myDropDown " as a DropDownList object and adding some items to it and it gives an error: "Object reference not set to an instance of an object".
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    @navyjax2,

    I'm not sure what you are trying to do with the HtmlForm.
    You should be loading your controls in an ASP.NET page, not an HTML Form.

    -Frinny

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Well you cannot load ASP.NET controls into an HTML form. It doesn't make sense. ASP.NET pages are executed to generate HTML that is sent to the browser. You have some control over the HTML generated.

      It really doesn't make sense to add an ASP.NET control to a HTML form.

      It makes a lot more sense to take the HTML generated by the ASP.NET control and add it to the HTML. You would do this by overwriting the Render code. You would do this if you were creating a server-control (as apposed to a user control)...but even then it would be rare to have a reason to overwrite the Render code.

      Does this make sense?

      I don't understand why the DropDownList wouldn't dynamically load in the page itself? It's pretty straightforward ...it would help if you showed what you were trying to do (if you were adding it to the HTML form, then it should not work)

      I cannot come up with any ideas because I cannot see any broken code posted ;)

      -Frinny
      Last edited by Frinavale; Mar 6 '12, 05:38 PM.

      Comment

      • navyjax2
        New Member
        • Aug 2007
        • 18

        #4
        Yeah, sorry, it's on a computer at my other office, but I think I remember it pretty clearly as I burned out a Saturday on it (this past weekend).

        What I was trying to do with the HtmlForm was something like was done in a blog posted here, just with adding dropdown items from SharePoint list items instead of buttons from XML items, but the concept is the exact same, so it seemed there was a proof of concept out there, already:

        Code:
        private void Page_Load(object sender, System.EventArgs e)
        { 
            XPathDocument doc = new XPathDocument(Server.MapPath("xmlfile1.xml"));
            XslTransform trans = new XslTransform();
            trans.Load(Server.MapPath("xsltfile1.xslt"));
        
            System.IO.StringWriter writer = new System.IO.StringWriter();
            trans.Transform(doc,null,writer);   
            writer.Flush();
            System.Text.StringBuilder sb = writer.GetStringBuilder();
        
            System.Web.UI.Control parsedControl = Page.ParseControl(sb.ToString());
            HtmlForm form = (HtmlForm)Page.FindControl("WebForm1");
            for (int i=0;i< parsedControl.Controls.Count;i++)
            {
                HtmlInputButton button = parsedControl.Controls[i] as HtmlInputButton;
                if(null != button)
                {        
                    button.ServerClick += new System.EventHandler(this.Button_Click);
                    form.Controls.Add(button);      
                }
            }
        }
        So that didn't work for adding my dropdown. I tried putting it in Page_Init and adding it to a panel, just as you suggested, and while that worked later for what I did in the User Control, it didn't work here.

        What I tried was doing something like this:

        Code:
        protected void Page_Init(object sender, EventArgs e)
        {
            string[] myVals;
            string[] myText;
        
            // pretend there is code here to return my list items
            // into the array objects above... too lengthy to type 
            // or read, but I did not have to modify it to get it 
            // working, so I know it wasn't the problem
        
            DropDownList ddl = new DropDownList();
            for (int i=0;myVals.length;i++)
            {
                ddl.Items.Add(new ListItem(myText[i],myVals[i]);
            }
            ddl.AutoPostBack = true;
            myPagePanel.Controls.Add(ddl);
        }
        And doing this (or something pretty close - I don't have the exact code in front of me right now) would not render my control to "myPagePane l", the asp:Panel I created on my page (probably didn't have that ID, but you get the idea).

        When I just moved that same exact code to my User Control, and added the User Control dynamically to the main page through it's Page_Init like you did, all was happy in the land of Nod. Not sure why.

        -Tom

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          That is strange.
          I'm glad it worked for you in the end.
          What works in a UserControl should work in a Page as well.

          -Frinny

          Comment

          Working...