RegisterOnSubmitStatement after client-side valida

2020-07-02 11:17发布

问题:

I need to insert a bit of Javascript into the process when a Web Form is submitted, but after the client side validation takes place.

RegisterOnSubmitStatement seems to place the javascript before the validation.

Anyone know how to get it to render after?

Solution found: In a web control, I put something like this:

protected override OnInit(EventArgs e) { 
   Page.SaveStateComplete += new EventHandler(RegisterSaveStuff); 
   base.OnInit(e); 
} 

void RegisterSaveStuff(object sender, EventArgs e) { 
   Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "name", "JS code here"); 
} 

回答1:

that´s right, the RegisterOnSubmitStatement DO NOT WORK in the init function. It should be called after in the page lige cycle. I thing the right place therefor is:

"PreRenderComplete"

protected override OnInit(EventArgs e) 
{ 
   Page.PreRenderComplete+= new EventHandler(Page_PreRenderComplete); 
   base.OnInit(e); 
} 

void Page_PreRenderComplete(object sender, EventArgs e) 
{ 
   Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "name", "JS code here"); 
}


回答2:

After some research online and playing around with it, I figured out that you can do it by hooking into the Page's SaveStateComplete event. I guess the validation submit statement is registered during the PreRender event, so if you register it after that (in the SaveStateComplete event) you can get it afterward.

You do have to re-register it, but that's not a big deal because I'm not relying on ViewState for my JS.