0
votes

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

2
well if it's only in the page behind, you should have Div1 added in your generated class. - Laurent Lequenne

2 Answers

0
votes

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");
0
votes

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