Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 自定义Web控件-ParseChildren&;资源对象_Asp.net_Controls - Fatal编程技术网

Asp.net 自定义Web控件-ParseChildren&;资源对象

Asp.net 自定义Web控件-ParseChildren&;资源对象,asp.net,controls,Asp.net,Controls,我希望有人能帮助我。我有以下自定义服务器控件: [ParseChildren(true)] public class MyControl : WebControl, INamingContainer { [PersistenceMode(PersistenceMode.InnerProperty)] public string MyProperty { get;set; } } 它与以下标记完美配合: <acme:MyControl runat

我希望有人能帮助我。我有以下自定义服务器控件:

[ParseChildren(true)]
public class MyControl : WebControl, INamingContainer
{
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public string MyProperty
    {
      get;set;
    }
}
它与以下标记完美配合:

<acme:MyControl runat="sever">
    <MyProperty>Test String</MyProperty>
</acme:MyControl>

测试字符串
但是如果我尝试本地化属性字符串,我会得到一个解析错误:

<acme:MyControl runat="sever">
    <MyProperty><%=(string)GetLocalResourceObject("MyResourceKey") %></MyProperty>
</acme:MyControl>

即使我强制转换类型,ASP.NET也表明该属性不能接受控件作为子控件。如果要对表达式进行本地化,表达式应该是什么样子?我可以将属性作为控件标记的属性进行访问,但我更喜欢上面的标记,它看起来更优雅、更干净


谢谢

添加第二个名为
MyPropertyResourceKey
的属性,这样您的最终代码将是:

[ParseChildren(true)]
public class MyControl : WebControl, INamingContainer
{
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public string MyProperty
    {
      get;set;
    }

    string _propertyResourceKey;
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public string MyPropertyResourceKey
    {
      get {
         return _propertyResourceKey;
      }
      set {
         _propertyResourceKey = value;

         MyProperty = (string)GetLocalResourceObject(_propertyResourceKey);
      }
    }
}
编辑: 根据评论,似乎还需要更灵活的方法。通常,如果要为控件指定动态值,则需要创建数据绑定控件(如果适用)


另一方面,如果控件不知道通常如何分配它的属性,则可接受的方法是在页面的代码隐藏中分配属性值。

我认为当控件在控件的每个实例中仅使用一个资源对象时,这会起作用,但如果字符串可以从另一个方法派生,该怎么办,不一定来自资源对象?