Link to home
Start Free TrialLog in
Avatar of jmwheeler
jmwheeler

asked on

RequiredFieldValidator not working in custom control

I am trying to create a custom control that is a TextBox with a RequiredFieldValidator.  The RequiredFieldValidator is to display a small image when the TextBox is empty.  When the page loads the small image is already being displayed.  Entering a value in the TextBox is not causing the small image to disappear.  I don't think the RequiredFieldValidator is being properly linked to the TextBox.  Below is the code that is rendering the controls.  Any information that can shed some light on this problem would be very helpful.


protected override void RenderContents(HtmlTextWriter output)
        {
            TextBox t = new TextBox();
            RequiredFieldValidator r = new RequiredFieldValidator();
            Image i = new Image();

            //Set textbox properties
            t.Text = Text;
            t.ID = TextBoxID;  //TextBoxID is a property defined elsewhere in the class

            //Set image properties
            i.ID = "img" + TextBoxID;
            i.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "AHS.HealthTrackWeb.UI.images.Error.png");
            i.AlternateText = ErrorMessage;  //ErrorMessage is a property defined elsewhere in the class
            i.Attributes.Add("style", "margin: 2px 2px 0 2px;");

            //Set validator properties
            r.ID = "rfv" + ID;
            r.SetFocusOnError = true;
            r.Display = Display;  //Display is a property defined elsewhere in the class
            r.EnableClientScript = true;
            //r.ErrorMessage = ErrorMessage;  //ErrorMessage is a property defined elsewhere in the class
            r.Controls.Add(i);
            r.ControlToValidate = t.ID;
           
            //Output results
            t.RenderControl(output);
            r.RenderControl(output);
           
        }
Avatar of Rejojohny
Rejojohny
Flag of United States of America image

you might want to have a look at this code on how to implement a image for a requriedfieldvalidator ..http://www.codeproject.com/aspnet/RequiredTextBox.asp

Rejo
Hi,

This is what u have to do

  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"  ><img src="../Images/1.GIF" /> </asp:RequiredFieldValidator>
 
Avatar of jmwheeler
jmwheeler

ASKER

Rejojohny: - I tried that and I am still having the same problem.

madhevan_pillai: - I know how to use a RequiredFieldValidator, my problem is that I am trying to create a custom control that wraps the textbox and rfv together.  It seems to be rendering everything but for some reason it always displays the validator even when something is in the TextBox.
ASKER CERTIFIED SOLUTION
Avatar of jmwheeler
jmwheeler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am confused how you could have tried what is mentioned in the link (which is by the way a working sample) and still have a problem .. it does everything that you want and more .. anyway, glad that you got it working ..

Rejo
no objections .. but I am not happy ..

the author asked for this ..
>> I am trying to create a custom control that is a TextBox with a RequiredFieldValidator.  The RequiredFieldValidator is to display a small image when the TextBox is empty.
>>I don't think the RequiredFieldValidator is being properly linked to the TextBox.  Below is the code that is rendering the controls.  Any information that can shed some light on this problem would be very helpful.

From the link I provided above
>>For my first article, I will show you all how I went about creating my RequiredTextBox, comprising a System.Web.UI.WebControls.TextBox and a customised RequiredFieldValidator. The finished product is a drag and drop text box that is set as a required field, and has an error provider look and feel similar to the System.Windows.Forms.ErrorProvider.
>>The approach taken above simply replaces the *normal* Text value of the RequiredFieldValidator with an image tag that has an embedded image.

The sample available in that site also has a working code which does exactly what is mentioned above ... so I was confused why that did not work ..

and I could not find anything in the link provided by the author which explains how the question could have been answered ..

Rejo
The answer posted by rejo did not work when merged with the code that I already had.  The control only worked when I overrid the CreateChildControls() method and created my controls inside as posted in the answer that I found on my own.  I appreciate the suggested article but it did not work for me.
thanks for clearing that up .. Your explanation will surely help others who refer to this thread for similar issues..

Rejo
Not a problem.  Thank you again for your assistance.
Closed, 250 points refunded.
Vee_Mod
Community Support Moderator