Is it possible to define a control to have non-specified set of attributes? For instance:
<MyPrefix:MyControl SomeAttribute="SomeValue" runat="server"/>
I don’t want to define a property on the control class beforehand for “SomeAttribute.” I’d really just like a HashTable or some other construct like this:
"SomeAttribute" => "SomeValue"
So this control can be used in many places with attributes that are essentially made up at runtime.
I’m wonder if there’s some parsing method I can override which iterates through the attributes at parse time. I can:
- Look for a property with the name and set it
- If I don’t find such a property, put the attribute name and value into a HashTable
Possible?
You want to use the IAttributeAccessor interface.
Example control:
Invocation:
Output:
Caveats:
It seems that SetAttribute is only called on attributes that can not be resolved normally. This means you’ll not see the id- or the runat-attribute in your code. Assigned properties (attr=”<%= DateTime.Now %>”) show up as an empty string. Databound properties does not show up at all in design mode, but works in normal mode (assuming that someone called DataBind, as usual).