简体   繁体   English

从aspx页面中的usercontrol访问id

[英]Access an id from usercontrol in aspx page

So i have the usercontrol named footer which contains the navigation to the website, I'm trying to append the active css class to the div in usercontrol so that the user 所以我有一个名为footer的usercontrol,其中包含对网站的导航,我正在尝试将active css类附加到usercontrol中的div,以便用户

This is my footer.ascx 这是我的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>

I've used this footer in my aspx page via 我在我的aspx页面中使用过这个页脚

<div id="footer"><ucl:Footer ID="foot" runat="server" /></div>

Now i want to add the css class active to the button id on their respective pages . 现在我想将css类active添加到各自页面上的button id So far i've tried adding the code down to the respective pages and it doesn't seem to work 到目前为止,我已经尝试将代码添加到相应的页面,它似乎不起作用

<script type="text/javascript">
    var x = document.getElementById("ImgActions");
    x.classname += " " + active;
</script>

what should i do to access the id of tag in the usercontrol in my aspx page. 我该怎么做才能在我的aspx页面中访问usercontrol中的tag标识。

You need to use ClientID of the control because the id of control will be changed when html is generated by asp.net as you do not have ClientIDMode not set to static . 您需要使用控件的ClientID ,因为当您没有将ClientIDMode设置为static时,asp.net生成html时将更改控件的ID。 also classname should be className classname也应该是className

var x = document.getElementById("<%= ImgActions.ClientID %>");
x.className += " " + active;

ASP.NET provides multiple algorithms for how to generate the ClientID property value. ASP.NET为如何生成ClientID属性值提供了多种算法。 You select which algorithm to use for a control by setting its ClientIDMode property. 您可以通过设置其ClientIDMode属性来选择要用于控件的算法。 The algorithms are identified by the ClientIDMode enumeration, Reference. 算法由ClientIDMode枚举Reference指定。

You can use ClientIDMode static as you have javascript in different control that does not know ImgActions. 您可以使用ClientIDMode静态,因为您在不同的控件中有不知道ImgActions的javascript。

Html 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 使用Javascript

 var x = document.getElementById("ImgActions");
 x.className += " " + active;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM