C # - error using FindControl on id div

I have an ASP.NET site where I am trying to access div elements by their identifier from C # code behind a file. Essentially, I want to see if a div element exists, and if so, change its properties.

I found many resources that point to a dozen different solutions, and none of them seem to work.

HTML on an ASP.Net page:

<div class="contentArea"> <div class="block" id="button1" runat="server"> Some Content Here </div> <div class="block" id="button2" runat="server"> Some Content Here </div> <div class="block" id="button3" runat="server"> Some Content Here </div> </div> 

C # Code Behind (examples I tried):

 System.Web.UI.HtmlControls.HtmlGenericControl div1 = (System.Web.UI.HtmlControls.HtmlGenericControl)this.FindControl("button1"); div1.Attributes["class"] = "classNameHere"; 

or

 Control div1 = this.FindControl("button1"); div1.GetType(); 

When the code falls into the second line of each of the above examples, I get an error message:

The reference to the object is not installed in the instance of the object.

If I try the following:

 if (div1 != null) { // Do Something; } 

Nothing happens because div1 is always null. Ironically, if I look at the Locals window and look at it, I can see the # id button in the list, so I know that they are, but the system acts as if it does not find control.

My ultimate goal is to find the max id # of the div buttons (looking at my html example, max id will be 3 (button3). Perhaps there is a better way to do this, but anyway, once I have my max id, I want to be able to touch each div and change some css properties.

Although I could easily do all this through jQuery, in this case I need to do this in C #.

Any help is greatly appreciated. If you need more information, let me know.

UPDATE I created a new C # web project from scratch. After adding the main page (and without changing it) and adding the web form using the main page, I added only one line to the web form in Content ID = "Content2":

 <div id="button1"></div> 

From the C # code behind, I still come across the same question as before.

FINAL UPDATE AND ANSWER I am shocked that no one (including me) caught my mistake from the above update. I never set runat = "server" when I created a new project from scratch under the div. Here's how I fixed my problem under my new project from scratch:

Add runat = "server" to the div:

 <div id="button1" runat="server"></div> 

Then I did a FindControl in the ContentPlaceHolder under MasterPage:

 ContentPlaceHolder myPlaceHolder = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1"); 

Note. This ContentPlaceHolder code appears on the Site.Master default page:

 <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> 

After searching for this ContentPlaceHolder in the code behind, I then searched inside this placeholder for button1:

 using System.Web.UI.HtmlControls; HtmlControl myControl = (HtmlControl)myPlaceHolder.FindControl("button1"); 

Finally, I check if myControl is null:

 if (myControl != null) { \\ Do Something } 

When I ran this code, it found the div that I was looking for. Here is the complete code behind everything:

 using System.Web.UI.HtmlControls; ContentPlaceHolder myPlaceHolder = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1"); HtmlControl myControl = (HtmlControl)myPlaceHolder.FindControl("button1"); if (myControl != null) { // Do Something } 
+7
source share
4 answers

If your page uses MasterPage, the div control will not be in the main collection of controls. This collection contains Content controls that point to the ContentPlaceholder of your MasterPage.

There are three options:

  • Use FindControl in the content control: contentControl.FindControl("button1");
  • Make a recursive FindControl until you find the required control
  • Typically, the declaration of your div control is added to your code.conf constructor, so you can directly access the control by its name: button1.Attributes["class"] = "classNameHere";

Update

I created MasterPage, added a Content page to it, and added <div id="button1" runat="server">Some text</div> to the content page.

In the code code of my content page, I added this code:

 protected void Page_Load(object sender, EventArgs e) { var control = FindHtmlControlByIdInControl(this, "button1"); if (control != null) { control.Attributes["class"] = "someCssClass"; } } private HtmlControl FindHtmlControlByIdInControl(Control control, string id) { foreach (Control childControl in control.Controls) { if (childControl.ID != null && childControl.ID.Equals(id, StringComparison.OrdinalIgnoreCase) && childControl is HtmlControl) { return (HtmlControl)childControl; } if (childControl.HasControls()) { HtmlControl result = FindHtmlControlByIdInControl(childControl, id); if (result != null) return result; } } return null; } 

This works for me.

+11
source

You need to add the runat = server attribute for any control you want to access in the code. See this article for more details.

Access to <div> with both javascript and code

0
source

I can't seem to reproduce your problem, is it likely that your DIV is in a template like UpdatePanel, Repeater or Grid?

0
source

If you add runat=server , add it at the end of the div:

It will work

 <div id="divBtn" class="mybuttonCass" runat="server"> 

However it will not

 <div runat="server" id="divBtn" class="mybuttonCass"> 

Thus, you can change any property from codebehind as:

 divBtn.Style["display"] = "none"; 

This works even with masterpages. Div is not included in the main page.

0
source

All Articles