代码之家  ›  专栏  ›  技术社区  ›  NeilC

自定义Web控件-分析子对象和资源对象

  •  0
  • NeilC  · 技术社区  · 16 年前

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

    [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>
    

    谢谢

    1 回复  |  直到 16 年前
        1
  •  0
  •   Oleks    15 年前

    添加第二个名为 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);
          }
        }
    }
    

    编辑: 根据评论,似乎还需要一种更灵活的方法。通常,如果您要为控件分配动态值,则需要创建数据绑定控件(如果适用) http://msdn.microsoft.com/en-us/library/ms366540.aspx .

    推荐文章