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
Solved

Dynamically added control to placeholder c# asp.net 2.0

Posted on 2007-03-23
4
306 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
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
mzalewski earned 250 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 250 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

828 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