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) {
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) {