Matt Grande
Matt Grande

Reputation: 12157

Client-side validation is not firing on my custom BaseValidator

So, I'm writing a custom validator to validate the Ajax Toolkit's Rating control. The client side validation isn't firing, though.

I have this method to register the script:

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    string script = @"<script type=""text/javascript"">function RatingValidatorEvaluateIsValid(val)
{
    alert( 'here' );
    return false;
}</script> ";

    Page.ClientScript.RegisterClientScriptBlock(GetType(), "ratingValidator", script);
}

And I register the attribute like this:

protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
{
    base.AddAttributesToRender(writer);

    // Add the client-side code (if needed)
    if (this.RenderUplevel)
    {
        writer.AddAttribute("evaluationfunction", "RatingValidatorEvaluateIsValid", false);
    }
}

The script appears on the page, as well as the attribute, but the alert('here') never appears.

(Everything works fine, Server-side)

Edit Markup:

<ajaxToolkit:Rating ID="ajaxRating" runat="server" StarCssClass="ratingStar" WaitingStarCssClass="savedRatingStar" 
                FilledStarCssClass="filledRatingStar" EmptyStarCssClass="emptyRatingStar" CssClass="stars"
                RatingAlign="Horizontal" RatingDirection="LeftToRightTopToBottom" 
                AutoPostBack="True" OnChanged="OnChanged" />
<my:RatingValidator runat="server" ID="RatingValidator" ControlToValidate="ajaxRating"
ErrorMessage="Please select a rating." EnableClientScript="True"
</my:RatingValidator>

Edit 2 Okay, this is weird...

The span that gets generated is proper:

<span id="CPHUser_rating_RatingValidator" evaluationfunction="RatingValidatorEvaluateIsValid" style="visibility:hidden;">Please select a rating.</span>

But the JS at the bottom doesn't have my evaluationfunction assigned.

var CPHUser_rating_RatingValidator = document.all ? document.all["CPHUser_rating_RatingValidator"] : document.getElementById("CPHUser_rating_RatingValidator");
CPHUser_rating_RatingValidator.controltovalidate = "CPHUser_rating_ajaxRating";
CPHUser_rating_RatingValidator.errormessage = "Please select a rating.";

Upvotes: 1

Views: 1371

Answers (1)

Matt Grande
Matt Grande

Reputation: 12157

Looks like I answered my own question...

I was apparently assigning my evaluationfunction incorrectly.

I've changed it to this:

Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "evaluationfunction", "RatingValidatorEvaluateIsValid");

and now it works.

Thanks for your help, guys!

Upvotes: 1

Related Questions