为什么此代码中的 ASP 标记内的 DIV 无效?
下面是我的网络内容表单的代码。 ASP 标记内的 DIV 显示绿色下划线,并且 DIV 标记中写入的文本在内容页上不可见。
<%@ Page Title="" Language="C#" MasterPageFile="~/Presentation_Layer/Pages/home.Master" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="live1._default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<asp:Label ID="lblWelcomeMessage" runat="server" ForeColor="Black">
<div id="divProfile">
This is a test.
</div>
</asp:Label>
</asp:Content>
Below is the code of my Web Content Form. The DIV inside ASP tag shows green underline and also the text that is written in DIV tag is not visible on the Content Page.
<%@ Page Title="" Language="C#" MasterPageFile="~/Presentation_Layer/Pages/home.Master" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="live1._default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<asp:Label ID="lblWelcomeMessage" runat="server" ForeColor="Black">
<div id="divProfile">
This is a test.
</div>
</asp:Label>
</asp:Content>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在浏览器上呈现为span
标记。您不能将块级元素嵌套在
inline
元素内。标签
和span
是
内联
元素,DIV
是块级元素。<asp:Label runat="server"/>
renders asspan
tag on browser.You cannot nest a block level element inside of an
inline
element.label
andspan
are
inline
elements,DIV
is block level.拉米兹的回答是正确的。如果您正在寻找解决方案,这里有一些选项:
呈现。 id 不容易猜到,但如果需要使用客户端脚本访问它,可以设置 CssClass。
另一种选择是,如果您想在代码隐藏中访问它,您可以这样做:
。如果您需要使用客户端脚本访问它,则只需设置 class 属性,而不是 CssClass 属性。
Ramiz's answer is correct. If you're looking for solutions, here are a few options:
<asp:Panel>
renders a<div>
. The id won't be easily guessable, but you can set the CssClass if you need to access it using client-side script.Another option is that you can just do this:
<div id="divProfile" runat="server">
if you want to have access to it within your codebehind. If you need to access this with client-side script, you just set the class attribute, not the CssClass attribute.