我正在创建一个带有关联客户端 API 的 ASP.NET 服务器控件。
在我的 GetScriptDescriptors() 方法中,我关联了一个名为“行”的属性...
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');
}
}
但是这会导致以下错误...
Error: Sys.InvalidOperationException: 'rows' is not a writable property.
似乎需要以下 setter 才能使 $create 语句为“行”分配初始值:
set_rows: function(value)
{
this._rows = value;
},
如果 setter 需要从 AddProperty 调用分配值,我如何在客户端 API 中将“行”属性设置为只读?
最佳答案
最简单的方法是让 set_rows 忽略任何输入。实际上,这应该完全阻止异常发生,并且仍然为该属性提供只读功能。
set_rows: function(value)
{
value = null;
},
关于c# - ScriptControlDescriptor.AddProperty 和只读属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2463955/