【问题标题】:ScriptControlDescriptor.AddProperty & Read-Only PropertiesScriptControlDescriptor.AddProperty & 只读属性
【发布时间】:2011-01-28 16:02:58
【问题描述】:

我正在创建一个带有关联客户端 API 的 ASP.NET 服务器控件。

在我的 GetScriptDescriptors() 方法中,我关联了一个名为“rows”的属性...

descriptor.AddProperty("rows", this.IntRows);

在我的客户端 API 中,我希望我的“行”属性是只读的...

MyControl = function(element)
{
    MyControl.initializeBase(this, [element]);
    this._rows;
}

MyControl.prototype =
{
    initialize: function()
    {
        MyControl.callBaseMethod(this, 'initialize');
    },

    get_rows: function()
    {
        return this._rows;
    },

    dispose: function()
    {
        MyControl.callBaseMethod(this, 'dispose');
    }
}

但是这会导致以下错误...

错误:Sys.InvalidOperationException: 'rows' 不是可写属性。

为了让 $create 语句为“rows”分配它的初始值,似乎需要以下设置器:

set_rows: function(value)
{
    this._rows = value;
},

如果需要设置器从 AddProperty 调用中分配值,我如何在客户端 API 中将“rows”属性设为只读?

【问题讨论】:

    标签: c# asp.net client-side servercontrols


    【解决方案1】:

    最简单的方法是让 set_rows 忽略任何输入。实际上,这应该完全阻止异常的发生,并且仍然为属性提供只读功能。

    set_rows: function(value)
    {
        value = null;
    },
    

    【讨论】:

    • 感谢您的回复。我还没有机会测试它。但是我不确定我是否遵循您的示例。如果调用了setter,那不是简单地清除“rows”值吗?
    • @Poz 你是对的。我将错误的选项设置为空。另一种选择是抛出未实现的异常,但这需要由编码器处理。而只是默默地忽略这个集合。
    • 感谢您的澄清。
    猜你喜欢
    • 2010-11-22
    • 2014-08-22
    • 2015-02-01
    • 2015-05-17
    • 2011-11-08
    • 2018-06-04
    • 2015-09-03
    • 1970-01-01
    相关资源
    最近更新 更多