简体   繁体   English

在runat =“server”之后仍然无法访问Asp.net div

[英]Asp.net div still not accessible after runat=“server”

I have a div element in my HTML. 我的HTML中有一个div元素。 I added a id and runat attributes to the element: 我在元素中添加了idrunat属性:

<div id="footer" runat="server"> 

After rendering, viewing the HTML shows: 渲染后,查看HTML显示:

<div id="ctl00_footer">

However, I cannot access it from the page's .aspx.cs code: 但是,我无法从页面的.aspx.cs代码访问它:

footer.InnerHtml += "test";

How do I access that element from the C# code? 如何从C#代码访问该元素?

您可以使用FindControl(“页脚”),并将其强制转换为HtmlGenericControl。

HtmlGenericControl footer = (HtmlGenericControl)FindControl("footer")

One of the reasons can be that you don't have designer file for that page. 其中一个原因可能是您没有该页面的设计器文件。 So you can't access element by it's ID . 所以你不能通过它的ID访问元素。

Just add class with name [your page name].aspx.designer.cs , open it, remove all code, save it, go to your view and click save - designer must generate code of all elements from your view. 只需添加名称为[您的页面名称] .aspx.designer.cs的类 ,打开它,删除所有代码,保存它,转到您的视图并单击保存 - 设计者必须从您的视图中生成所有元素的代码。 After this you can access element by ID . 在此之后,您可以按ID访问元素。

There should be no problem accessing <div id="footer" runat="server"></div> the way you are doing. 访问<div id="footer" runat="server"></div>应该没有问题。 Strange though, my generated markup keeps the div id unchanged as footer . 奇怪的是,我生成的标记使div id保持不变为footer
Make sure you don't have any compile errors, and that you can access other elements running server-side in the same scope you are trying to access this div. 确保您没有任何编译错误,并且您可以在尝试访问此div的同一范围内访问运行服务器端的其他元素。

You need to set the ClientIDMode property of the page or control to Static: 您需要将页面或控件的ClientIDMode属性设置为Static:

http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx

This will prevent the "ctl00_" from being appended to the ID which is what is causing you the problem. 这样可以防止“ctl00_”被附加到导致问题的ID上。

I have encountered this problem before. 我之前遇到过这个问题。 You may have the target div inside another div that does not have the runat="server" attribute. 您可能将目标div放在另一个没有runat="server"属性的div中。 All nested divs should have the runat attribute in order to be able to access the inner elements. 所有嵌套的div都应该具有runat属性,以便能够访问内部元素。

<div id="divContainer" runat="Server">
    <div id="yourDiv" runat="Server">
</div>

If you're going to code ASP.NET and you want to access the control from the server-side, you may as well use the provided controls. 如果您要编写ASP.NET代码并且想要从服务器端访问控件,那么您也可以使用提供的控件。

Use a Panel instead of a Div. 使用Panel而不是Div。 The ASP:Panel control renders as a div in the generated html anyway. 无论如何,ASP:Panel控件在生成的html中呈现为div。 The Panel doesn't have a .Text property, but you can add controls to it from code-behind (such as a Label or a LiteralControl . Panel没有.Text属性,但您可以从代码隐藏(例如LabelLiteralControl)向其添加控件。

Is there possibly a chance that the page was copy/pasted when being created? 是否有可能在创建页面时复制/粘贴页面? If so, make absolutely sure that all references to the old page are changed to the name of the new page. 如果是这样,请确保对旧页面的所有引用都更改为新页面的名称。 I've done this before within the code at the top of the ASPX page, as well as the namespace of the designer page. 我之前在ASPX页面顶部的代码中以及设计器页面的命名空间中完成了这个。

I had the same problem and found out that due to a "copy" and "paste" my function had a "static" declaration. 我有同样的问题,发现由于“复制”和“粘贴”我的功能有一个“静态”声明。 Removed it since static functions can't access non-static identifiers and it was fixed. 删除它,因为静态函数无法访问非静态标识符并且已修复。

I have the same issue. 我有同样的问题。 My solution was to have 'ID' instead of 'id' for the div element (ie the casing was the reason). 我的解决方案是为div元素设置'ID'而不是'id'(即外壳是原因)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM