Adding validation controls programmatically

Discussion in 'ASP .Net Building Controls' started by D Sheldon, May 11, 2004.

  1. D Sheldon

    D Sheldon Guest

    I am creating a server control that adds web controls (i.e. textboxes, etc) to a form. I use HtmlTable to build the table and insert the controls. Now I want to add validators to the textbox. Here is the code that I am using:

    bool lastnamerequired=false;
    public bool LastNameRequired
    get{return lastnamerequired;}
    set{lastnamerequired = value;}

    System.Web.UI.WebControls.RequiredFieldValidator rfv = new RequiredFieldValidator();
    rfv.ControlToValidate = "sFirstName";
    rfv.Display = ValidatorDisplay.Dynamic;
    rfv.Text = "";
    rfv.ErrorMessage = "Required";
    rfv.EnableClientScript = true;
    rfv.ID = "rfvFirstName";

    This code seems to only add the word "Required" to my form when rendered. It's not dynamic nor does it go away when the field is valid.

    Any ideas?
    D Sheldon, May 11, 2004
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.