ModalPopupExtender and ScriptManager AsyncPostBackError Event

M

mohaaron

Hello all,

I have been using the ModalPopupExtender as a way of catching errors
and displaying them to users. This includes business rule violations
that I catch. This all works fine as long as I can write the if
statement to trap the error.

However this does not work in the case of un-handled exceptions. I
have recently found out that un-handled ajax exceptions can be dealt
with using the AsyncPostBackError event. So what I have been trying to
do is deal with the error using the AsyncPostBackError event and this
display a custom message using the ModalPopupExtender. What I'm
finding now is that the ModalPopupExtender does not work after an un-
handled ajax exception. Apparently the processing of the page stops
after the error so I can't call the UpdatePanel.Update() and
ModalPopupExtender.Show() methods.

I would really like to be able to re-use the ModalPopupExtender for
all error messages. Can anyone tell me how I might solve this problem?

Thanks
 
Joined
Jul 14, 2011
Messages
1
Reaction score
0
Solution

I know this is REALLY OLD but...I ran into this as well. I'm going to post what I've done to make it work just in case in the future I run across this issue again and need to figure it out again :wink:.

Here's what needs done...

#1 - Add a "master.js"
Code:
var prm = Sys.WebForms.PageRequestManager.getInstance();
if (prm != null) {
	prm.add_endRequest(masterEndRequest);
}

function masterEndRequest(s, e) {
	try {
		if (e._error && !e._errorHandled) {
			var message = e._error.description
				.replace(e._error.name, "")
				.replace(":", "")
				.replace(/^\s+|\s+$/g, "");
			$get(errorTextId).innerHTML = message;
			$find("errorModal").show();
			e._errorHandled = true;
		}
	} catch (e) { throw e; }
}

#2 - Configure ScriptManager
HTML:
<asp:ScriptManager ID="scriptManager" runat="server" LoadScriptsBeforeUI="False"
	OnAsyncPostBackError="OnAsyncPostBackError">
	<Scripts>
		<asp:ScriptReference Path="Master.js" />
	</Scripts>
</asp:ScriptManager>

#3 - Create the ModelPopup content
This could be done as a UserControl or within the MasterPage markup. The important part here is that we're using a script tag to set up a variable with the clientID of the placeholder for our Error Message.
HTML:
<asp:Panel ID="pnlError" runat="server" CssClass="error-dialog" ViewStateMode="Disabled">
    <asp:Panel ID="pnlErrorTitle" runat="Server" CssClass="error-title">Errors Occured</asp:Panel>
	<div class="error-summary">
	<asp:UpdatePanel ID="upnlError" runat="server" UpdateMode="Conditional">
		<ContentTemplate>
	<asp:Label ID="lblErrorText" runat="server" ClientIDMode="Static" />
		</ContentTemplate>
	</asp:UpdatePanel>
	</div>
	<div class="buttons">
		<asp:Button ID="btnAccept" runat="server"
			CssClass="button"
			Text="<%$ Resources:Resources, OkButtonText %>" 
			UseSubmitBehavior="False"
			/>
	</div>
</asp:Panel>
<asp:Button ID="errorTarget" runat="server" ViewStateMode="Disabled" style="display: none" />
<asp:ModalPopupExtender ID="errorModal" runat="server" 
	BackgroundCssClass="progress-background"
	BehaviorID="errorModal"
	Drag="True" 
	EnableViewState="False" 
	PopupControlID="pnlError" 
	PopupDragHandleControlID="pnlErrorTitle" 
	TargetControlID="errorTarget" 
	OkControlID="btnAccept">
</asp:ModalPopupExtender>

<script type="text/javascript">
	var errorTextId = "<%= this.lblErrorText.ClientID %>";
</script>

#4 - Test and enjoy!
:congrats:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top