[英]Access an id from usercontrol in aspx page
所以我有一个名为footer
的usercontrol,其中包含对网站的导航,我正在尝试将active
css类附加到usercontrol中的div,以便用户
这是我的footer.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Footer.ascx.cs" Inherits="UserControls_Footer" %>
<div id="bottom">
<div class="Footer navbar-fixed-bottom">
<div class="container">
<div class="row">
<button id="ImgOption1Guru" class="btn btn-large btn-block btn-primary" runat="server" type="button" onclick="location.href='Option1.aspx';">Option 1</button>
<button id="ImgCmnyInfo" class="btn btn-large btn-block btn-primary" onclick="location.href='CompanyInfo.aspx';" runat="server" type="button">Info</button>
<button id="ImgInteract" class="btn btn-large btn-block btn-primary" onclick="location.href='Interact.aspx';" runat="server" type="button">Interact</button>
<button id="ImgActions" class="btn btn-large btn-block btn-primary" onclick="location.href='Actions.aspx';" runat="server" type="button"> Actions</button>
<button id="ImgStatus" class="btn btn-large btn-block btn-primary" onclick="location.href='Status.aspx';" runat="server" type="button">Status</button>
</div>
</div>
</div>
</div>
我在我的aspx
页面中使用过这个页脚
<div id="footer"><ucl:Footer ID="foot" runat="server" /></div>
现在我想将css类active
添加到各自页面上的button id
。 到目前为止,我已经尝试将代码添加到相应的页面,它似乎不起作用
<script type="text/javascript">
var x = document.getElementById("ImgActions");
x.classname += " " + active;
</script>
我该怎么做才能在我的aspx页面中访问usercontrol中的tag标识。
您需要使用控件的ClientID
,因为当您没有将ClientIDMode设置为static
时,asp.net生成html时将更改控件的ID。 classname
也应该是className
var x = document.getElementById("<%= ImgActions.ClientID %>");
x.className += " " + active;
ASP.NET为如何生成ClientID属性值提供了多种算法。 您可以通过设置其ClientIDMode属性来选择要用于控件的算法。 算法由ClientIDMode枚举Reference指定。
您可以使用ClientIDMode静态,因为您在不同的控件中有不知道ImgActions的javascript。
HTML
<button id="ImgActions" ClientIDMode="static" class="btn btn-large btn-block btn-primary" onclick="location.href='Actions.aspx';" runat="server" type="button"> Actions</button>
使用Javascript
var x = document.getElementById("ImgActions");
x.className += " " + active;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.