Ok, here’s a stupid question… I keep running into this from time to time and I waste a few minutes on this every time I do and I’ve never really resolved this properly.
If I have a control that inherits say from a Panel control (which basically generates a <div>) and I want to override the width to a different default value I do:
/// <summary>
/// Override default Width
/// </summary>
[DefaultValue(typeof(Unit),"250px")]
public override Unit Width
{
get { return base.Width; }
set { base.Width = value; }
}
This works for showing the control with a width of 250px. Fine. But now if I go in make a change to the width in the designer, then tell the designer to reset the value it sets the designer value back to 250px but it generates a blank into the HTML markup:
<ww:wwDbResourceControl runat="server" ID="dbResourceManager"
CssClass="ErrorDisplay"
style="padding:10px">
Notice no width of 250. So at this point the control renders a full width <div> that spans the width of the form because it’s essentially rendering without a width at all, which is the base behavior.
So I tried override and new, but both show the same behavior. I also tried this:
/// <summary>
/// Override default Width
/// </summary>
[DefaultValue(typeof(Unit),"250px")]
public override Unit Width
{
get { return base.Width; }
set
{
if (value == Unit.Empty)
value = Unit.Pixel(250);
base.Width = value;
}
}
Which gives some really crazy behavior. Now when setting the empty value the control shows at 250 pixels but when I set to 250 pixels (or Reset in the designer) the control renders full width again.
I also tried using my own property and not calling back into base.Width, but that’s not really a good idea either I suspect and that doesn’t any more reliably because apparently the base behavior looks at the base value even if the override exists higher up the chain…
The only way I can get close to the desired behavior is to skip putting a [DefaultValue()] attribute on the control. When I do that the control always renders the tag and then value is properly reflected. But this shouldn’t really be necessary.
Question is how do I properly subclass the control to get the width to properly render at 250px when reset?
Other Posts you might also like