ASP.NET:如何从JScript中访问UserControl的属性?

Gif*_*guy 2 javascript asp.net user-controls properties

我需要从一个<script>元素内读取对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; }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Fra*_*nov 5

您的用户控件正在托管您的Web应用程序的服务器上执行.js正在客户端的机器浏览器中执行.js无法与您的用户控件进行交互.

但是,您的用户控件会在执行时发出html标记.该html包含在js可以访问的页面dom中.因此,您可以将该属性的值作为该html的一部分发出(例如,作为标记中的js变量声明).执行此操作的最佳位置是用户控件.ascx文件.


Jef*_*nal 5

正如Franci所写,你需要在服务器上构建页面时将属性的值写入html输出.

对于您的示例,这可能是最简单的方法:

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

(<%= %>构造只是简称Response.Write.)