2009-10-10 50 views
2

我需要從我的UserControl的<script>元素中讀取用戶定義的屬性。這個腳本需要在用戶點擊鏈接時運行(我也不知道如何設置)。謝謝你的建議,所以!ASP.NET:如何從JScript中訪問我的UserControl的屬性?

代碼隱藏:

public partial class TNLink : System.Web.UI.UserControl 
{ 
    [System.ComponentModel.BindableAttribute(true)] 
    public virtual string TNImageURL { get; set; } 

    [System.ComponentModel.BindableAttribute(true)] 
    public virtual string FullImageURL { get; set; } 

    [System.ComponentModel.BindableAttribute(true)] 
    public virtual string Caption { get; set; } 
} 

ASP:

<script type="text/javascript"> 
    //****How do I access the FullImageURL property for this line?**** 
    document.getElementById('imgFull').src = FullImageURL; 
</script> 
<div style="text-align: center"> 
    <a> 
     <asp:Image ID="imgTN" runat="server" 
      ImageUrl='<%# DataBinder.Eval (Page, "TNImageURL") %>' 
      style="border: 5px solid #000000; width: 85%;" /><br /> 
     <asp:Label ID="lblCaption" runat="server" 
      Text='<%# DataBinder.Eval (Page, "Caption") %>' /> 
    </a> 
</div> 

回答

5

由於Franci wrote,您需要在構建服務器上的頁面時將屬性值寫入您的html輸出。

這可能是做你的榜樣最簡單的方法:(該<%= %>結構僅僅是短暫的Response.Write

<script type="text/javascript"> 
    document.getElementById('imgFull').src = '<%= FullImageURL %>'; 
</script> 

5

你的用戶控件承載您的Web應用程序在服務器上執行。 js正在客戶端的機器瀏覽器中執行。 js無法與您的用戶控件進行交互。

但是,您的用戶控件在其執行過程中確實發出了html標記。該html包含在js可以訪問的頁面dom中。因此,您可以將該屬性的值作爲該html的一部分(例如,作爲標記中的js變量聲明)。最好的地方是用戶控制.ascx文件。

+0

「因此,你可以發出與物業價值部分的HTML「 - 我怎麼去做這件事? – Yatrix 2012-07-06 21:51:51

1

當您運行該頁面時,查看html源代碼(右鍵單擊查看源代碼)。您會看到那裏沒有<asp>標籤。全部轉換爲<html>標籤。

對於如:

<asp:Panel>替換成HTML <div>。如果你想訪問面板,你只能使用Javascript訪問div。

對於其他用戶控件也是如此。檢查相應的html。看看你能做什麼。或者爲我們提供html輸出。

相關問題