簡體   English   中英

從aspx頁面中的usercontrol訪問id

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM