我有一个带有以下控件的 asp 网站:
<span id="expTrainingShow" class="clsLink" style="margin-left: 20px;" onclick="GridChanger();">
+ Show Expired Continuing Education</span>
我想根据后面代码中设置的条件隐藏它。我可以访问这样的 span id 吗? (网站是用visual basic搭建的)
最佳答案
您可以使用 Label
而不是 html-span(它也呈现为 span)或者您可以添加 runat="server"
。设置 runat="server"
允许您像访问任何其他服务器控件一样通过其 ID 访问代码中的 HTML 元素。
<span id="expTrainingShow" runat="server" class="clsLink" style="margin-left: 20px;" onclick="GridChanger();" ></span>
代码隐藏中的某处(跨度是服务器端的 HtmlGenericControl
):
expTrainingShow.InnerHtml = yourText ' set the text '
或
expTrainingShow.Visible = False ' hide it '
请注意,服务器端的 Visible=False
意味着该控件根本不会在客户端呈现,因此它不存在于 html 中,只能在服务器端访问。
如果您只是想隐藏它但无论如何都要渲染它,您应该使用 CSS 或 expTrainingShow.Style.Add("display","none")
。
关于asp.net - 如何在后面的代码中访问 span id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12290332/