exposing a style property

J

Jeremy Chapman

I've built a custom control which basically includes a textbox and a
validatorcontol as child controls.

(Code included below)

I've exposted the style of the validator as a property of my control called
ValidatorStyle so I can access it at design time. I've set the property to
persist as content, so that changes persist in the aspx page.

By default the ForeColor property of the validator style is red and the
ValidatorStyle property is persisted as <ValidatorStyle ForeColor="Red" />.
If I set the CssClass of ValidatorStyle to a value of "Error" the property
is persisted as <ValidatorStyle ForeColor="Red" CssClass="Error" />. which
is correct. My problem manifests itself though when I blank out the
ForeColor property of the validator style. Switching to the source view of
my page I can see that the ValidatorStyle property persisted as
<ValidatorStyle CssClass="Error" />, but if I switch back to design view, I
see that the validator control still appears red and it also appears red at
runtime. The ForeColor property value in the design mode says "red" even
though the aspx source does not specify the value. This is a problem
because If I want to set the CssClass property to a class that has a
forecolor, the css class's forecolor will always be overridded with red,
because the style values take precidence over the css property values. I
think the correct behavior would be for the ValidatorStyle property to
persist as <ValidatorStyle CssClass="error" ForeColor=""/> when the
ForeColor is blanked out. How can I get this to happen?

[DefaultProperty("Text")]
[ToolboxData("<{0}:MyControl runat=server></{0}:MyControl>")]
[ValidationProperty("Text")]
public class TimePicker : System.Web.UI.WebControls.WebControl,
INamingContainer
{
private TimePickerValidator pValidator_m;
private BaseTextBox pText_m;

[PersistenceMode(PersistenceMode.InnerProperty)]
[Category("Styles")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[NotifyParentProperty(true)]
[DefaultValue((string)null)]
[Browsable(true)]
public Style ValidatorStyle
{
get
{
EnsureChildControls();
return pValidator_m.ControlStyle;
}
}

protected override void CreateChildControls()
{
base.EnsureID();
if (pText_m == null)
{
pText_m = new BaseTextBox();
pText_m.ID = "txttim";

pText_m.MaxLength = 0;
pText_m.Columns = 0;
pText_m.TextMode = TextBoxMode.SingleLine;
Controls.Add(pText_m);
}

if (pValidator_m == null)
{
pValidator_m = new TimePickerValidator();
pValidator_m.ID = "valcal";
Controls.Add(pValidator_m);
}
base.CreateChildControls();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top