views:

134

answers:

1

I hope someone can help me. I have the following custom server control:

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

It works perfectly with the following mark-up:

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

But if I try to localise the property string, I get a parse error:

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

Even if I cast the type, ASP.NET indicates that the property cannot accept controls as children. How should the expression look like if I want to localise it? I can make the property accessible as an attribute of the control's tag, but I prefer the mark-up above, it looks more elegant and clean.

Thanks

A: 

Add a second property called MyPropertyResourceKey, so your final code would be:

[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);
  }
}

}

EDIT: Based on comments, it seems that there may also be a demand for a more flexible method. Generally, if you are looking assign a dynamic value to a control, you will want to either create a Data Bound control (if applicable) http://msdn.microsoft.com/en-us/library/ms366540.aspx.

If, on the other hand, the control does't know how its will usually be assigned, the accepted method is to assign the property value in the code-behind of the page.

MrGumbe
I suppose that would work when the control will only use one resource object in every instance of the control, but what if the string could be derived from another method, not necessarily from a resource object?
Raybiez