0

I have a div within the body of my page, and it looks like this -

<div runat="server" class="row" style="display:none; height:600px;" id="Div1">
</div>

I have the following code to try and access it in the page behind -

HtmlGenericControl getLargeDiv = (HtmlGenericControl)this.FindControl("Div1");
if (getLargeDiv != null)
{
    getLargeDiv.Style.Add("display", "inherit");
}

Yet it seems to find Null everytime. I cant quite work it out. The div is sat within a panel, which does contain a runat="server" - this affecting it?

Thanks

VDWWD
  • 35,079
  • 22
  • 62
  • 79
sam smith
  • 107
  • 1
  • 8

2 Answers2

0

You are probably working with a Master Page. If so you need to find the ContentPlaceHolder first.

ContentPlaceHolder cph = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
HtmlGenericControl getLargeDiv = (HtmlGenericControl)cph.FindControl("Div1");

Or as one-liner

HtmlGenericControl getLargeDiv = (HtmlGenericControl)Master.FindControl("ContentPlaceHolder1").FindControl("Div1");
VDWWD
  • 35,079
  • 22
  • 62
  • 79
0

You might want to run a recursive method that iterate through the control tree. FindControlRecursive