exposing a style property

Discussion in 'ASP .Net Building Controls' started by Jeremy Chapman, Mar 24, 2006.

  1. 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();
    }
    }
     
    Jeremy Chapman, Mar 24, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sonic
    Replies:
    1
    Views:
    1,956
    John Saunders
    Jan 7, 2005
  2. Paul
    Replies:
    1
    Views:
    427
  3. daokfella
    Replies:
    1
    Views:
    330
    bruce barker
    Aug 8, 2008
  4. Deep S.

    Exposing Font Name Property for Custom Control

    Deep S., Aug 4, 2003, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    183
    Deep S.
    Aug 4, 2003
  5. Exposing an event handler as a control property

    , Aug 28, 2006, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    168
    studen771
    Sep 30, 2006
Loading...

Share This Page