如何在代码隐藏页面中选择div元素? [英] How to select a div element in the code-behind page?
问题描述
我有一个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"
注意:
-
Div不包含
runat ="server"
属性.
这不是母版页文件,而是另一个名为"AssignImages.aspx"的文件,其中包含ContentPlaceHolder.
This is not the master page file but this is another file named "AssignImages.aspx" and it contains ContentPlaceHolder.
div在此ContentPlaceHolder中:
The div is inside this ContentPlaceHolder:
<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server" ID="Content1">
推荐答案
如果要从后面的代码中找到控件,则必须在控件上使用 runat ="server"
属性.然后,您可以使用 Control.FindControl
.
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
.
<div class="tab-pane active" id="portlet_tab1" runat="server">
Control myControl1 = FindControl("portlet_tab1");
if(myControl1!=null)
{
//do stuff
}
如果您使用Runat服务器,并且控件位于 ContentPlaceHolder
内部,则必须知道ctrl名称不再是portlet_tab1.它将以ctrl00格式呈现.
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.
类似于:#ctl00_ContentPlaceHolderMain_portlet_tab1.如果使用jquery,则必须修改名称.
Something like: #ctl00_ContentPlaceHolderMain_portlet_tab1. You will have to modify name if you use jquery.
您也可以在客户端使用jQuery,而无需使用runat-server属性:
You can also do it using jQuery on client side without using the runat-server attribute:
<script type='text/javascript'>
$("#portlet_tab1").removeClass("Active");
</script>
这篇关于如何在代码隐藏页面中选择div元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!