Reputation: 9413
My asp.net page dynamically displays 207 questions (I can't control this). Each question have 10 validators. When ASP renders the page it creates following three lines for each validator:
var qsnQuestionSet_adult_qcQuestion_1_vldMaxAnswersValidator = document.all ? document.all["qsnQuestionSet_adult_qcQuestion_1_vldMaxAnswersValidator"] : document.getElementById("qsnQuestionSet_adult_qcQuestion_1_vldMaxAnswersValidator");
qsnQuestionSet_adult_qcQuestion_1_vldMaxAnswersValidator.display = "Dynamic";
qsnQuestionSet_adult_qcQuestion_1_vldMaxAnswersValidator.evaluationfunction = "CustomValidatorEvaluateIsValid";
Althrough these three lines are just 4kb you can imagine that 4*10*207 is quite a lot. How can I mark all validators as dynamic and set evaluationfunction to same value without asp generating the line for me?
Upvotes: 4
Views: 148
Reputation: 5450
This javascript is rendered to the client by the AddAttributesToRender() method of the BaseValidator & CustomValidator classes from the System.Web.UI.WebControls namespace. Take a look at them in Reflector.
Protected Overrides Sub AddAttributesToRender(ByVal writer As HtmlTextWriter)
...
If (enumValue <> ValidatorDisplay.Static) Then
Me.AddExpandoAttribute(writer2, clientID, "display", PropertyConverter.EnumToString(GetType(ValidatorDisplay), enumValue), False)
End If
...
MyBase.AddExpandoAttribute(writer2, clientID, "evaluationfunction", "CustomValidatorEvaluateIsValid", False)
End Sub
You could write your own class to replace CustomValidator and change the way that it renders. But, in this case I think it would be better to write your own javascript to handle validation and not use the validator controls.
P.S. If you're worried about the size of the HTML the first thing you should do is enable gzip compression on your IIS server.
Upvotes: 1
Reputation: 16994
This code is generated automatically by ASP.NET when the EnableClientScript option is set to true. As far as I'm aware the only way to get rid of it would be to set this to false however the obvious drawback is the validation will only happen on the server side during a postback.
To get around this you could tie the custom javascript validation function to the related control event such as a textbox onBlur event but without knowing more detail about what values you are trying to validate it is difficult to speculate further as to whether this could be a solution.
Upvotes: 2