如何从javascript的属性用户控件获取数据?

时间:2012-02-06 07:48:28

标签: c# javascript asp.net .net

我有一个用户控件:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Autocomplite.ascx.cs" Inherits="Application.Controls.Autocomplite" %>

<script type="text/javascript" src="../Scripts/Autocomplite.js"></script>
<img alt="" src="Images/Common/transfer1.png" style="cursor:pointer;" onclick='SelectInfo(this); return false;' />

有属性:

public partial class Autocomplite : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {            

        }

        private string tableDataSourse;
        public string TableDataSourse
        {
            get { return tableDataSourse; }
            set { tableDataSourse = value; }
        }

        private string positionId;
        public string PositionId
        {
            get { return positionId; }
            set { positionId = value; }
        }
    }

我在页面上注册此控件并按以下方式运行:

<%@ Register TagPrefix="apress" TagName="Autocomplite" Src="~/Controls/Autocomplite.ascx" %>
...
<apress:Autocomplite ID="AutoTextBox1" runat="server" TableDataSourse="DataSet" PositionId="NewOrderArticleCode" />

在SelectInfo函数的Autocomplite.js(javascript客户端)上,我尝试获取此属性:

function SelectInfo(obj) {
    if (obj != null) {
    // expected that obj have this properties
    }
}

1 个答案:

答案 0 :(得分:0)

您应该将这些属性的值渲染为html元素或属性(例如输入元素或图像元素的自定义属性)。那么你可以通过客户端代码获取这些值。

将控制级属性渲染为html代码:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Autocomplite.ascx.cs" Inherits="Application.Controls.Autocomplite" %>
<script type="text/javascript" src="../Scripts/Autocomplite.js"></script>

<img alt="" src="Images/Common/transfer1.png"
style="cursor:pointer;" onclick='SelectInfo(this); return false;'
data-property1='<%= TableDataSourse %>' data-property2='<%= PositionId %>' />

访问渲染属性:

function SelectInfo(obj) {
    return $(obj).attr('data-property1') ;
    }
}