Solved

Dynamically added control to placeholder c# asp.net 2.0

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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