?
Solved

RequiredFieldValidator not working in custom control

Posted on 2007-10-15
11
Medium Priority
?
1,322 Views
Last Modified: 2008-01-09
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);
           
        }
0
Comment
Question by:jmwheeler
10 Comments
 
LVL 26

Expert Comment

by:Rejojohny
ID: 20078838
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
0
 
LVL 12

Expert Comment

by:madhevan_pillai
ID: 20083367
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>
 
0
 
LVL 11

Author Comment

by:jmwheeler
ID: 20084948
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Accepted Solution

by:
jmwheeler earned 0 total points
ID: 20085171
Nevermind,  

I found information that worked for me.

http://msdn2.microsoft.com/en-us/library/aa478969.aspx
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 20085470
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
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 20092861
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
0
 
LVL 11

Author Comment

by:jmwheeler
ID: 20097648
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.
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 20097897
thanks for clearing that up .. Your explanation will surely help others who refer to this thread for similar issues..

Rejo
0
 
LVL 11

Author Comment

by:jmwheeler
ID: 20108963
Not a problem.  Thank you again for your assistance.
0
 
LVL 1

Expert Comment

by:Vee_Mod
ID: 20117528
Closed, 250 points refunded.
Vee_Mod
Community Support Moderator
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question