【问题标题】:Why are my property get_ and set_ not exposed in an AJAX Control?为什么我的属性 get 和 set 没有在 AJAX 控件中公开?
【发布时间】:2009-08-17 09:42:50
【问题描述】:

我正在尝试创建 AJAX 控件,但在控件中看不到属性 get_ 和 set_ 方法。

这是我的 .js 文件中的代码:

Type.registerNamespace('MyCompany.ControlLibrary');

MyCompany.ControlLibrary.WebNotify = function(element)
{
    // Module level variables
    this._message = '';

    //Calling the base class constructor
    MyCompany.ControlLibrary.WebNotify.initializeBase(this, [element]);
}


MyCompany.ControlLibrary.WebNotify.prototype =
{
    //Getter for Message Property
    get_message : function()
    {
        return this._message;
    },

    //Setter for Message Property
    set_message : function(value)
    {debugger;
        var e = Function._validateParams(arguments, [{name: 'value', type: String}]);
        if (e) throw e;

        if (this._message != value)
        {
            // Only sets the value if it differs from the current
            this._message = value;
            //Raise the propertyChanged event
            this.raisePropertyChanged('message'); //This is a base class method which resides in Sys.Component
        }
    },

    initialize : function()
    {
        //Call the base class method
        MyCompany.ControlLibrary.WebNotify.callBaseMethod(this, 'initialize');
    },

    dispose : function()
    {
        //Call the base class method
        MyCompany.ControlLibrary.WebNotify.callBaseMethod(this, 'dispose');
    }
}





MyCompany.ControlLibrary.WebNotify.registerClass('MyCompany.ControlLibrary.WebNotify', Sys.UI.Control);

if (typeof(Sys) != 'undefined')
{
    Sys.Application.notifyScriptLoaded();
}

这是我在 .cs 文件后面的代码中的属性:

[DescriptionAttribute("The message that is displayed in the notifier.")]
public string Message
{
    get { return _message; }
    set { _message = value; }
}
private string _message = "No Message Specified";

编辑:这是我的全部代码:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using System.Web.Services;
using System.Collections.Generic;

namespace MyCompany
{
    [DefaultProperty("ID")]
    [ToolboxData("<{0}:WebNotify runat=server />")]
    public class WebNotify : Button, IScriptControl
    {


#region Constructors

        public WebNotify()
        {

        }

#endregion


#region Page Events


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected override void OnPreRender(EventArgs e)
        {

            if (!this.DesignMode)
            {
                ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
                if (scriptManager != null)
                {
                    scriptManager.RegisterScriptControl(this);
                }
                else
                    throw new ApplicationException("You must have a ScriptManager on the Page.");
            }

            base.OnPreRender(e);

        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (!this.DesignMode)
            {
                ScriptManager.GetCurrent(this.Page).RegisterScriptDescriptors(this);
            }
            base.Render(writer);
        }

        protected override void CreateChildControls()
        {

            base.CreateChildControls();

        }



#endregion


#region Properties


        [DescriptionAttribute("The message that is displayed in the notifier.")]
        public string Message
        {
            get { return _message; }
            set { _message = value; }
        }
        private string _message = "No Message Specified";



#endregion



#region IScriptControl


        IEnumerable<ScriptDescriptor> IScriptControl.GetScriptDescriptors()
        {
            ScriptControlDescriptor desc = new ScriptControlDescriptor("MyCompany.WebNotify", ClientID);
            desc.AddProperty("message", this.Message);
            yield return desc;
        }

        IEnumerable<ScriptReference> IScriptControl.GetScriptReferences()
        {
            yield return new ScriptReference(Page.ResolveUrl("~/WebNotify.js"));
        }


#endregion


    }
}

【问题讨论】:

  • 只是补充一下,这是我试图调用客户端来检索/更新属性的代码: var notifier = $get('ntfTest'); notifier.set_message(文本);

标签: c# asp.net javascript ajax


【解决方案1】:

缺少 [ExtenderControlProperty] 属性的代码

【讨论】:

  • 我似乎没有...我是参考 System.Web.Extensions 等。它驻留在哪个命名空间中?
  • AjaxControlToolkit.ExtenderControlPropertyAttribute
  • 我已经添加了,但它仍然没有出现在我的方法或属性列表中。
  • 还有一个——这些属性是区分大小写的,所以正确的JS:get_Message, set_Message
  • 已经试过了。还有其他想法吗?如果我在 JS 调试器模式下查看对象,我什至看不到我的 get_ 和 set_ 方法。我只能看到标准属性。
【解决方案2】:

我不需要任何 AJAX Control Kit 的东西。我只需要使用 $find 而不是 $get。现在我使用 $find 一切正常。

真是头疼。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-09
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    相关资源
    最近更新 更多