C#-在div ID上使用FindControl时出错 [英] c# - error using FindControl on div id
问题描述
我有一个ASP.NET网站,我试图通过文件后面的C#代码中的div元素访问其div元素.本质上,我想查看div元素是否存在,如果存在,请更改其属性.
I have an ASP.NET site that I am trying to access div elements by their ID from the C# code behind file. Essentially I want to see if a div element exists, and if so, alter its properties.
我发现那里有很多资源,这些资源指向许多不同的解决方案,但似乎没有一个起作用.
I've found many resources out there that point to a dozen different solutions and none of them seem to work.
ASP.Net页面上的HTML:
<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#后面的代码(我尝试过的示例):
System.Web.UI.HtmlControls.HtmlGenericControl div1 = (System.Web.UI.HtmlControls.HtmlGenericControl)this.FindControl("button1");
div1.Attributes["class"] = "classNameHere";
或
Control div1 = this.FindControl("button1");
div1.GetType();
当代码到达上述每个示例的第二行时,我得到一个错误:
When the code gets to the second line of each of the above examples, I get an error:
对象引用未设置为对象的实例.
Object reference not set to an instance of an object.
如果我尝试以下操作:
if (div1 != null)
{
// Do Something;
}
什么都没有发生,因为div1始终设置为null.具有讽刺意味的是,如果我查看Locals窗口并进行检查,则可以在清单中看到button#ID,因此我知道它们在那里,但是系统的行为就像找不到控件一样.
Nothing ever happens because div1 is always set to null. Ironically, if I look at the Locals window and examine this, I can see the button# ids in the listing, so I know they are there, but the system is acting like it isn't finding the control.
我的最终目标是找到按钮div的最大ID号(在我的html示例中,最大ID将为3(button3).也许有更好的方法可以解决此问题,但是无论哪种方式,一次我有自己的最大ID,我希望能够触摸每个div并更改一些CSS属性.
My ultimate goal is to find the max id # of the button divs (looking at my html example, the max id would be 3 (button3). Maybe there is a better way to go about it, but either way, once I have my max id, I want to be able to touch each div and alter some css properties.
尽管我可以通过jQuery轻松完成所有这些操作,但在这种情况下,我需要在C#中执行此操作.
Although I could easily do all of this via jQuery, in this instance I need to do this in C#.
我们非常感谢您的帮助.如果您需要更多信息,请告诉我.
Any help is much appreciated. If you need more info, let me know.
更新我从头开始创建了一个新的C#Web项目.在添加母版页(并且不对其进行更改)并使用母版页添加Web表单之后,我仅在Content ID ="Content2"下的Web表单中添加了一行:
UPDATE I created a new C# web project from scratch. After adding a masterpage (and not altering it) and adding a webform using masterpage, I only added one line to the webform under Content ID="Content2":
<div id="button1"></div>
从后面的C#代码开始,我仍然遇到与以前完全相同的问题.
From c# code behind I still run into the same exact issue as before.
最终更新和答案我感到震惊的是,没有人(包括我自己)从上述更新中发现我的错误.当我在div下从头开始创建新项目时,我从未放过runat ="server".这是我从头开始解决新项目下的问题的方法:
FINAL UPDATE AND ANSWER I'm shocked no one (including myself) caught my mistake from the above update. I never put runat="server" when I created a new project from scratch under the div. Here is how I fixed my problem under my new project from scratch:
将runat ="server"添加到div:
Add runat="server" to div:
<div id="button1" runat="server"></div>
然后我在MasterPage下的ContentPlaceHolder上执行了FindControl:
Then I did a FindControl on the ContentPlaceHolder under the MasterPage:
ContentPlaceHolder myPlaceHolder = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
注意:这是默认创建的Site.Master页面上ContentPlaceHolder代码的样子:
Note: This is what the ContentPlaceHolder code looks like on the Site.Master page created by default:
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
在后面的代码中找到此ContentPlaceHolder之后,我在此占位符中搜索button1:
After finding this ContentPlaceHolder in code behind, I then searched within this placeholder for button1:
using System.Web.UI.HtmlControls;
HtmlControl myControl = (HtmlControl)myPlaceHolder.FindControl("button1");
最后,我检查myControl是否为空:
Finally I check to see if myControl is null:
if (myControl != null)
{
\\ Do Something
}
当我运行这段代码时,它找到了我正在寻找的div.这是所有内容汇总在一起的完整代码:
When I ran this code, it found the div I was looking for. Here is the complete code behind all put together:
using System.Web.UI.HtmlControls;
ContentPlaceHolder myPlaceHolder = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
HtmlControl myControl = (HtmlControl)myPlaceHolder.FindControl("button1");
if (myControl != null)
{
// Do Something
}
推荐答案
如果您的页面使用的是MasterPage,则div控件将不在控件的主要集合中.该集合仅包含指向您的MasterPage的ContentPlaceholder的Content控件.
If your page is using a MasterPage, the div control will not be in the main collection of controls. That collection only contains the Content controls pointing to the ContentPlaceholder of your MasterPage.
共有三个选项:
- 在Content控件上使用
FindControl
:contentControl.FindControl("button1");
- 执行递归
FindControl
,直到找到所需的控件 - 通常,将div控件的声明添加到designer.cs代码的后面,因此您可以直接通过其名称访问该控件:
button1.Attributes ["class"] ="classNameHere";
- Use
FindControl
on the Content control:contentControl.FindControl("button1");
- Do a recursive
FindControl
until you find the control you need - Normally, a declaration of your div control is added to your designer.cs codebehind, so you can directly access the control by its name:
button1.Attributes["class"] = "classNameHere";
更新
我创建了一个母版页,向其中添加了内容页,并向内容页添加了< div id ="button1" runat ="server">某些文本</div>
在内容页面的代码中,添加了以下代码:
In the codebehind 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;
}
这对我有用.
这篇关于C#-在div ID上使用FindControl时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!