0

I have a table that includes 27 DropDownLists for user input. My table has 27 occurrences of this HTML:

<span id="s1" runat="server"><asp:PlaceHolder ID="p1" runat="server"></asp:PlaceHolder></span>

where the spans are indexed s1, s2, ..., s27 and the PlaceHolders are indexed p1, p2, ..., p27. The reason that the spans are indexed is so that I can replace the DropDownList with whatever selection was made -- ie, the DropDownList will disappear.

Here is how I am generating the DropDownLists:

protected void Page_Load(object sender, EventArgs e)
{
    var data = CreateDataSource();
    int x;
    for (x = 1; x <= 27; x++)
    {
        DropDownList dl = new DropDownList();
        string index = x.ToString();
        dl.ID = "TrendList" + index;
        dl.AutoPostBack = true;
        dl.SelectedIndexChanged += new EventHandler(this.Selection_Change);
        dl.DataSource = data;
        dl.DataTextField = "TrendTextField";
        dl.DataValueField = "TrendValueField";
        dl.DataBind();
        if (!IsPostBack)
        {
            dl.SelectedIndex = 0;
        }
        PlaceHolder ph = (PlaceHolder)form1.FindControl("p" + index);
        ph.Controls.Add(dl);
    }
}

A runtime error occurs at the last line. I can select any DropDownList I want and make a selection, but when I select a second DropDownList and make a selection I get this error:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Line 46:             }
Line 47:             PlaceHolder ph = (PlaceHolder)form1.FindControl("p" + index);
Line 48:             ph.Controls.Add(dl);
Line 49:         }

This seemed to be working when I was doing it by brute force:

p1.Controls.Add(DropList1);
p2.Controls.Add(DropList2);
etc....

but now I am getting an error. I have run this in the debugger but I can't find a null reference.

Any advice is appreciated.

Regards.

svick
  • 236,525
  • 50
  • 385
  • 514
Kevin
  • 1,252
  • 6
  • 32
  • 50

4 Answers4

0

Have you tried just using one placeholder? The error message seems to be about how there isn't a placeholder by the ID of "p" + index.ToString()

Eric
  • 1,356
  • 2
  • 14
  • 24
0

The placeholders are not technically in form1, they're in the spans that are in form1 (Or the spans are in some other control, etc.).

This would work in the situation where the spans are nested in form1:

var s = form1.FindControl("s" + index);
var ph = s.FindControl("p" + index);
ph.Controls.Add(dl);
hmqcnoesy
  • 4,165
  • 3
  • 31
  • 47
  • Thanks. I tried this and I got the same null pointer exception. – Kevin Jun 12 '12 at 13:36
  • @Kevin - if you want to post the entire markup (all of form1, anyway), I'll take a look and see if I can come up with the right code. – hmqcnoesy Jun 12 '12 at 17:23
0

The FindControl method is not recursive, so you will have to use a way to iterate through nested objects. Here is a good example found here on SO: C#, FindControl

Community
  • 1
  • 1
Chris
  • 360
  • 1
  • 8
  • Thanks for the link. I tried searching recursively but I still get the same null pointer exception. – Kevin Jun 12 '12 at 13:37
0

The problem turned out to be that this function was called on every new page. Meaning that after the first run the first placeholder no longer existed and threw the null reference error. The problem was solved with this code:

if (placeHolder != null)
{
    placeHolder.Controls.Add(ddl);
}
Kevin
  • 1,252
  • 6
  • 32
  • 50