Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dynamically added control to placeholder c# asp.net 2.0

Posted on 2007-03-23
4
Medium Priority
?
310 Views
Last Modified: 2013-11-07
I want to dynamically add a control to an aspx page. I use ajax. I only know  the number of controls and what type they are at runtime (only textboxes and dropdowns)
I have three placeholders on my page, they are static.
I have a function that tries to add controls dynamically to these placeholders, but the bum out with an error saying "object reference not set to an instance of an object"
This is the function:
       public void GenDropDown()
      {
        DropDownList dl = new DropDownList();
        dl.DataSource = GetConfigurationList(4);
        dl.ID = "1";
        dl.DataTextField = "ChoiceDisplayedValue";
        dl.DataValueField = "ConfigurationAttributeChoiceId";
        dl.DataBind();
        PlaceHolder myPlaceHolder = (PlaceHolder)Page.FindControl("ph1");
        myPlaceHolder.Controls.Add(dl);
      }
This function works fine untill the last line, dl contains a dropdown with several items, but when I add it it complains with the error above.

I think this is related to the fact that the controls are not predefined from what I have found online.

I am open to suggestions if the example above is not workable.

I have fiddled with LoadControl, but I don't know how to use it for a webcontrol, it only seems to work for user defined controls (ascx).

Any help appreciated!.

Cheers,
Hugo
0
Comment
Question by:HugoForte
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
mzalewski earned 750 total points
ID: 18803644
So you're saying that you are getting an "object reference not set to an instance of an object" error on this line?
        myPlaceHolder.Controls.Add(dl);

The first thing I'd look at is myPlaceHolder. I'd guess that it knows is supposed to reference a PlaceHolder object, but the variable is empty (ie: it couldn't find the ph1 control in the previous line)
Try this:
if (myPlaceHolder != null)
        myPlaceHolder.Controls.Add(dl);

This should prevent that error (if my guess is right), but you won't notice any new drop down list.
If this is true, then you need to work out why it can't find the placeholders.
0
 
LVL 5

Assisted Solution

by:mzalewski
mzalewski earned 750 total points
ID: 18803673
You say the 3 placeholders are static?

You should be able to do this (note the reference to the placeholder above the method):
This will only work if it's in the codebehind/src of the page you have placed the placeholders on.

     public PlaceHolder ph1;
     public void GenDropDown()
      {
        DropDownList dl = new DropDownList();
        dl.DataSource = GetConfigurationList(4);
        dl.ID = "1";
        dl.DataTextField = "ChoiceDisplayedValue";
        dl.DataValueField = "ConfigurationAttributeChoiceId";
        dl.DataBind();

ph1.Controls.Add(dl);
      }

I remember when I was first learning ASP.NET, I used to get confused about why I had to declare controls that I wanted to access in the source when they already existed in the ASPX page itself.

Basically (from my understanding anyway), the src file is defining a class that the ASPX page *inherits*.  This class already contains a 'PlaceHolder ph1', so the one set up in the ASPX page is simply overriding this variable.

Hope this helps anyway
0
 

Author Comment

by:HugoForte
ID: 18819470
Thanks for your help.
The solution ended up being a bit more complex and I solved it myself before the comments came in. Thank you guys very much for your help though!

Aloha,
Hugo
0
 
LVL 1

Expert Comment

by:xMayhemx
ID: 20172785
Could you possibly share your solution?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question