Solved

Dynamically added control to placeholder c# asp.net 2.0

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

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 …
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 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