2009-10-10 51 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输出。

相关问题