Sunday, March 11, 2012

ModalPopup and UpdatePanel - how to handle the partial postback?

Hi Soccerdad,

soccerdad:

a) In my OnSubmit function (where I call show() on the modalpopupextender), if I can detect that the postback is going to be a partial postback, then I can just skip showing the modal popup altogether.

Here is the sample to detect the post type when onsubmit event occured.

 <form id="form1" runat="server" onsubmit="return onSubmitCheck()"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" /> </ContentTemplate> </asp:UpdatePanel> <asp:Button ID="Button2" runat="server" Text="Button2" /> <script type="text/javascript" language="javascript"> var prm = Sys.WebForms.PageRequestManager.getInstance(); function onSubmitCheck(){ if(!prm._postBackSettings.async){ alert('It is a synchronous post!'); }else{ alert('It is a synchronous post!'); } return true; } </script> </form>

soccerdad:

b) Alternatively, if I can "hook" the completion of the partial postback, I can call "hide()" on the modalpopupextender to make it disappear.

Please add the following code to your page. Function EndRequestHandler will be called when the partial refresh finishs.

<script type="text/javascript" language="javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function EndRequestHandler(sender, args){
//hide the ModalPopupExtender
}
</script>

I hope this help.

Best regards,

Jonathan


Thanks for the reply, Jonathan. It definitely fixed my issue - I used the first method, and am now not displaying the modal popup for a partial postback.

Unfortunately, that led me to find another issue. My submit hook function is called after validators have been run (the alert dialog appears) but even if the validation has failed. So the modal popup grays out the window and things are stuck like that. I've used what I think is a hack to determine if validation succeeded or not before showing the modal popup. I'm not sure if there's a "best practice" way to detect this under the asp.net ajax stuff, or if this is more of a general asp.net issue.

Anyway, thanks for the pointers.

Donnie



Hi Soccerdad,

If you use Javascript to validate the inputs, it is easy. You just make the validate function return true if the content of all the input is valid, otherwise return false. So do the onSubmit function.

If you use validators, I think the "best practice" is using the second solution.

I hope this help.

Best regards,

Jonathan


I got it working the "standard" way I think. I'm using the built-in asp.net validators. On pages which have those, a "Page_IsValid" global variable is set after the validators have been run. So I just check for the existence and value of that variable in my onsubmit hook function and react accordingly.

I found a reference to the use of this variable via a google search, and it seemed to be a documented and supported variable that I can use for this purpose, so I'm happy with the solution.

Thanks again

Donnie


Hi

Don't forget to add ValidationGroup attributes to each of the validators and to the button/link control that causes the page to postback. Tying all the related validators and the firing control with the same ValidationGroup name will stop those validators from interfering with any other controls that cause your page to postback.


No comments:

Post a Comment