Last post Oct 20, 2006 03:12 AM by JohanJesus
Oct 17, 2006 02:45 PM|JohanJesus|LINK
I want to get rid of the style-attributes (e.g color: red;)
Public Class AdaptedRequiredFieldValidator : Inherits System.Web.UI.WebControls.Adapters.WebControlAdapter
Dim rfv As RequiredFieldValidator
Protected Overrides Sub OnInit(ByVal e As EventArgs)
rfv = Control
Protected Overrides Sub RenderBeginTag(ByVal writer As HtmlTextWriter)
If rfv.CssClass = "" Then
Protected Overrides Sub RenderEndTag(ByVal writer As HtmlTextWriter)
Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
If rfv.Text = "" Then
Oct 17, 2006 04:12 PM|albertpascual|LINK
Oct 18, 2006 02:58 AM|JohanJesus|LINK
Oct 18, 2006 01:01 PM|Russ Helfand|LINK
The assumption when you build an adapter is that you are taking responsibility for both the rendering and the client-side behavior of the control. In some cases, you have to take over a lot of the processing on the server-side, too. That sort of makes
that your adapters aren't rendering. I think the framework decides that since it can't be certain, it simply won't render any of the JS that would have been used by the unadapted markup since you are taking charge of the markup through the adapter.
The bottom line is that you'll have to render your own markup in the adapter. You could do that by using the Page.ClientManager and its various "register" methods to register and use your own JS to do the validation.
However, I'm guessing you don't want to do that. You might want to stick with more traditional control derivation to fix your issue. Derive a new kind of validator from the existing framework class you are dissatisfied with. Then override its render method.
I think you can then spit out whatever SPAN, etc. you like but will still get the JS you wanted.
Oct 18, 2006 01:04 PM|Russ Helfand|LINK
Addendum... the class derivation strategy only works if you have control over the instantiation of the validator (i.e., its being done in your own markup). That way you can use your new class (as a tag that you register on your page) rather than the usual
If, however, your are ending up with the validator because it is internally used by some other tag (like a membership control) then you may need to stick with the adapter strategy because it is the only way to force the framework to automatically use your
version of that validator. In that case, you have to "own" the responsibility for the JS code. Sort of a bummer.
Oct 19, 2006 02:21 AM|JohanJesus|LINK
Ok, thanks. But that´s not what I wanted to hear [:(]
Why, why does the webcontrols use inline-styling, why? Let´s say I´ve got 500 validators in one project and decides not to use the color red anymore...
Oct 20, 2006 12:01 AM|Russ Helfand|LINK
I wonder if this is as simple as adding a default entry in your skin file to style all asp:requiredfieldvalidators so they use whatever color (etc.) that you prefer. See,
Though I tend to use SkinID to couple my skin entries to specific control instances, I think you can omit the SkinID and thereby define a default look for any control. I suppose that could include a validator.
Oct 20, 2006 03:12 AM|JohanJesus|LINK
That works. But the style is applied inline. I hope MS puts the css class after the style attribute in the future though.
One nice thing with writing my own adapter is that I can supply a default text and set setFocusOnError to true by default.