C# 如何在代码隐藏页面中选择一个 div 元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19265103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to select a div element in the code-behind page?
提问by Manu
I have a div element:
我有一个 div 元素:
<div class="tab-pane active" id="portlet_tab1">
I want to control this element from the code-behind page and remove the class "active"
我想从代码隐藏页面控制这个元素并删除类“活动”
NOTE:
笔记:
Div doesn't contain the
runat="server"
property.This is not the master page file but this is another file named "AssignImages.aspx" and it contains ContentPlaceHolder.
Div 不包含该
runat="server"
属性。这不是母版页文件,而是另一个名为“AssignImages.aspx”的文件,它包含 ContentPlaceHolder。
The div is inside this ContentPlaceHolder:
div 位于此 ContentPlaceHolder 内:
<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server" ID="Content1">
采纳答案by Carlos Landeras
If you want to find the control from code behind you have to use runat="server"
attribute on control. And then you can use Control.FindControl
.
如果你想从后面的代码中找到控件,你必须runat="server"
在控件上使用属性。然后你可以使用Control.FindControl
.
<div class="tab-pane active" id="portlet_tab1" runat="server">
Control myControl1 = FindControl("portlet_tab1");
if(myControl1!=null)
{
//do stuff
}
If you use runat server and your control is inside the ContentPlaceHolder
you have to know the ctrl name would not be portlet_tab1 anymore. It will render with the ctrl00 format.
如果您使用 runat 服务器并且您的控件在里面,ContentPlaceHolder
您必须知道 ctrl 名称将不再是 portlet_tab1。它将以 ctrl00 格式呈现。
Something like: #ctl00_ContentPlaceHolderMain_portlet_tab1. You will have to modify name if you use jquery.
类似于:#ctl00_ContentPlaceHolderMain_portlet_tab1。如果使用 jquery,则必须修改名称。
You can also do it using jQuery on client side without using the runat-server attribute:
您也可以在客户端使用 jQuery 来完成此操作,而无需使用 runat-server 属性:
<script type='text/javascript'>
$("#portlet_tab1").removeClass("Active");
</script>
回答by Rajesh Subramanian
You have make div as server control using following code,
您已使用以下代码将 div 作为服务器控件,
<div class="tab-pane active" id="portlet_tab1" runat="server">
then this div will be accessible in code behind.
那么这个 div 将可以在后面的代码中访问。
回答by Deep Sharma
Give ID
and attribute runat='server'
as :
给出ID
和属性runat='server'
为:
<div class="tab-pane active" id="portlet_tab1" runat="server">
//somecode Codebehind:
//一些代码隐藏:
Access at code behind
在代码后面访问
Control Test = Page.FindControl("portlet_tab1");
Test.Style.Add("display", "none");
or
portlet_tab1.Style.Add("display", "none");
回答by Fandango68
@CarlosLanderas is correct depending on where you've placed the DIV control. The DIV by the way is not technically an ASP control, which is why you cannot find it directly like other controls. But the best way around this is to turn it into an ASP control.
@CarlosLanderas 是正确的,具体取决于您放置 DIV 控件的位置。顺便说一下,DIV 在技术上不是 ASP 控件,这就是为什么您不能像其他控件一样直接找到它的原因。但解决这个问题的最好方法是将其转换为 ASP 控件。
Use asp:Panel instead. It is rendered into a <div>
tag anyway...
请改用 asp:Panel。<div>
无论如何,它被渲染成一个标签......
<asp:Panel id="divSubmitted" runat="server" style="text-align:center" visible="false">
<asp:Label ID="labSubmitted" runat="server" Text="Roll Call Submitted"></asp:Label>
</asp:Panel>
And in code behind, simply find the Panel control as per normal...
在后面的代码中,只需按照正常方式找到 Panel 控件...
Panel DivCtl1 = (Panel)gvRollCall.FooterRow.FindControl("divSubmitted");
if (DivCtl1 != null)
DivCtl1.Visible = true;
Please note that I've used FooterRow, as my "psuedo div" is inside the footer row of a Gridview control.
请注意,我使用了 FooterRow,因为我的“伪 div”位于 Gridview 控件的页脚行内。
Good coding!
好编码!
回答by JJ_Coder4Hire
you'll need to cast it to an HtmlControl in order to access the Style property.
您需要将其转换为 HtmlControl 以访问 Style 属性。
HtmlControl control = (HtmlControl)Page.FindControl("portlet_tab1"); control.Style.Add("display","none");
HtmlControl control = (HtmlControl)Page.FindControl("portlet_tab1"); control.Style.Add("display","none");
回答by Hudson
id + runat="server" leads to accessible at the server
id + runat="server" 导致在服务器上可访问