Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically added Validation control, formview and the cancel button

Status
Not open for further replies.

mcowen

Programmer
Oct 21, 2001
134
GB
I've dynamically added a RequiredFieldValidator control to a textbox in a formview. The first time the page loads I can see the script block hooking up the javascript function to the control...

Code:
 <script type="text/javascript">
//<![CDATA[
var FormView1_validator2 = document.all ? document.all["FormView1_validator2"] : document.getElementById("FormView1_validator2");
FormView1_validator2.controltovalidate ="FormView1_NameTextBox";
FormView1_validator2.errormessage ="Error";
FormView1_validator2.evaluationfunction ="RequiredFieldValidatorEvaluateIsValid";
FormView1_validator2.initialvalue = "";
//]]>
</script>
When the formview's cancel button is clicked and post back occurs, the page is reloaded and the code to dynamically add the validator is run again. The script above is not added though so client validation can't occur if update is then clicked.Does anyone know why this happens? What is different in the chain of events when cancel is clicked to those events on a page load?I've been looking at the RequiredFieldValidator's AddAttributesToRender method and to confuse me more, when I watch RenderUplevel it always comes back false - even on page load so how is the script ever added??

Code:
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
    base.AddAttributesToRender(writer);
    if (base.RenderUplevel)
    {
        string clientID = this.ClientID;
        HtmlTextWriter writer2 = base.EnableLegacyRendering ? writer : null;
        base.AddExpandoAttribute(writer2, clientID, "evaluationfunction", "RequiredFieldValidatorEvaluateIsValid", false);
        base.AddExpandoAttribute(writer2, clientID, "initialvalue", this.InitialValue);
    }
}
Thanks

Matt
 
if you need to dynamically add a validator why not add a validator control to the page, instead of munging control attributes?
Why do you need to add this control dynamically?

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I have some classes with validation on their properties. I'm trying to associate those validation attributes into the asp.net validation controls so that if I change those classes any changes to the validation is immediately reflected on the client and the server.
I've run into this when trying out the Validation Framework -
If the javascript is created fine on page load what could keep it from being generated after a postback?

Matt
 
because your not telling it to render with each request to the page. each request is atonomous. it has no knowledge of the previous request(s). that's the behavior of the web. postback and viewstate hack this model to allow for some form of state in a stateless enviornment.

I don't have much experience with the validation framework, but I don't think you need to override the AddAttributesToRender method just to supply entity validation. I was under the impression the framework required less friction to wire up validation.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top