ASP.NET:如何访问我的用户的属性,从内部的JScript? [英] ASP.NET: How do I access a property of my UserControl, from within JScript?

查看:139
本文介绍了ASP.NET:如何访问我的用户的属性,从内部的JScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要只读访问我的用户的用户定义的属性,从&LT之内;脚本> 元素。该脚本需要在用户点击链接(这也是我不知道如何设置)上运行。谢谢你的建议,这样的!

code-背后:

 公共部分类TNLink:System.Web.UI.UserControl
{
    [System.ComponentModel.BindableAttribute(真)
    公共虚拟字符串TNImageURL {搞定;组; }    [System.ComponentModel.BindableAttribute(真)
    公共虚拟字符串FullImageURL {搞定;组; }    [System.ComponentModel.BindableAttribute(真)
    公共虚拟字符串的标题{搞定;组; }
}

ASP:

 <脚本类型=文/ JavaScript的>
    // ****我如何访问FullImageURL属性这一行?****
    的document.getElementById('imgFull')SRC = FullImageURL。
< / SCRIPT>
< D​​IV的风格=文本对齐:中心>
    < A>
        < ASP:图片ID =imgTN=服务器
            的ImageUrl ='<%#DataBinder.Eval的(页,TNImageURL)%>'
            风格=边界:5像素固体#000000;宽度:85%; />< BR />
        < ASP:标签ID =lblCaption=服务器
            文字='<%#DataBinder.Eval的(页,标题)%GT;' />
    &所述; / A>
< / DIV>


解决方案

作为<一个href=\"http://stackoverflow.com/questions/1548549/asp-net-how-do-i-access-a-property-of-my-usercontrol-from-within-jscript/1548590#1548590\">Franci写,你需要的属性的值写入到HTML输出,同时构建服务器上的页面。

这可能是做到这一点的最简单的方式与您的例子:

 &LT;脚本类型=文/ JavaScript的&GT;
    的document.getElementById('imgFull')的src ='&下;%= FullImageURL%GT;';
&LT; / SCRIPT&GT;

(即&LT;%=%&GT; 构造仅仅是短暂的的Response.Write

I need read-access to a user-defined property of my UserControl, from within a <script> element. This script needs to run when the user clicks on the link (which I also don't know how to setup). Thanks for your advice, SO!

Code-behind:

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>

解决方案

As Franci wrote, you need to write the value of the property to your html output while constructing the page on the server.

This is probably the easiest way to do that with your example:

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

(The <%= %> construct is just short for Response.Write.)

这篇关于ASP.NET:如何访问我的用户的属性,从内部的JScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆