Solved

Dynamically added control to placeholder c# asp.net 2.0

Posted on 2007-03-23
4
303 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now