Inherited Textbox required validator

Discussion in 'ASP .Net' started by bcutting@gmail.com, Apr 27, 2005.

  1. Guest

    I have a control which inherits from TextBox and supplies some
    additional data functionality. I was hoping to include a boolean flag
    "Required" that can be set in the designer. When the flag is set to
    True the control should include a RequiredFieldValidator for itself.
    No matter how I add the validator within the control it never seems to
    show up in the page.

    protected override void CreateChildControls()
    {
    if(m_bRequiredField)
    {
    RequiredFieldValidator rfv = new RequiredFieldValidator ();
    rfv.ControlToValidate = this.ID;
    rfv.Display = ValidatorDisplay.Dynamic;
    rfv.ErrorMessage = UserFieldName + " is required.";
    this.Controls.Add(rfv);
    }
    base.CreateChildControls ();
    }


    I would like to avoid creating a composite control. I have also tried
    other events such as PreRender and DataBind.

    I even tried hacking the text HTML for the validator in the Render and
    add an empty validator to the Page.Validators with the same clientID.
    But the page doesn't seem to register it.

    my attempted hack

    protected override void OnPreRender(EventArgs e)
    {
    RequiredFieldValidator rvf = new RequiredFieldValidator ();
    rvf.ID=this.ClientID + "_Validator";
    Page.Validators.Add(rvf);
    base.OnPreRender (e);
    }
    protected override void Render(HtmlTextWriter writer)
    {
    base.Render (writer);
    if(m_bRequiredField)
    {
    writer.Write("<span id=\""+this.ClientID+"_Validator\" " +
    "controltovalidate=\"" + this.ClientID + "\" " +
    "errormessage=\""+UserFieldName+" is required\" display=\"Dynamic\"
    evaluationfunction=\"RequiredFieldValidatorEvaluateIsValid\" " +
    "initialvalue=\"\" style=\"color:Red;display:none;\">*</span>");
    }
    }


    Any ideas would be appreciated.
     
    , Apr 27, 2005
    #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. Replies:
    4
    Views:
    908
    Richter~9.6
    Feb 13, 2007
  2. Sehboo

    Inherited TextBox

    Sehboo, Jan 14, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    170
    Victor Garcia Aprea [MVP]
    Jan 14, 2004
  3. Richard Purchas

    Change in behaviour from framework version 1.1 to 2.0 for inherited textbox

    Richard Purchas, Feb 12, 2006, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    138
    Richard Purchas
    Feb 12, 2006
  4. Custom Composite Control inherited from TextBox

    , Oct 24, 2006, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    138
    John Saunders
    Oct 24, 2006
  5. 7stud --
    Replies:
    11
    Views:
    416
    7stud --
    Nov 9, 2007
Loading...

Share This Page