PDA

View Full Version : [CLOSED] AjaxEvents and Exception



Timothy
Sep 09, 2008, 7:34 PM
Hello,



Would it be possible to throw an exception from an AjaxEvent and capture it on the UI?



For example; if I clicked my button:






<ExtJS:Button ID="btnAddCustomer" runat="server" AutoPostBack="False" Text="Add Customer" SkinID="Default">
<AjaxEvents>
<Click OnEvent="btnAddCustomer_Click" />
</AjaxEvents>
</ExtJS:Button>



And code behind:


protected void btnAddCustomer_Click(object sender, AjaxEventArgs e)
{
throw new Exception("Hello World");
}


Would be similar to how you currently handle exceptions with the GridPanel :)

Cheers,
Timothy

geoffrey.mcgill
Sep 09, 2008, 9:00 PM
You have a couple options...

1. Don't return a Message and just handle the Failure. The following sample demonstrates handling the Failure response.

Example



<script runat="server">
protected void Button1_Click(object sender, AjaxEventArgs e)
{
throw new ApplicationException("An Exception was thrown on Button2");
}
</script>

<ext:Button
ID="Button1"
runat="server"
Text="Throw Exception">
<AjaxEvents>
<Click
OnEvent="Button1_Click"
Failure="!{'Warning', 'An error has occured'}"
/>
</AjaxEvents>
</ext:Button>

2. Catch the Exception server-side, and return a custom message and parameter.

Example


<script runat="server">
protected void Button2_Click(object sender, AjaxEventArgs e)
{
try
{
throw new ApplicationException("An Exception was thrown on Button1");
}
catch (ApplicationException ex)
{
e.UserParamsResponse["failure"] = ex.Message;
}
}
</script>

<ext:Button
ID="Button2"
runat="server"
Text="Handle Exception">
<AjaxEvents>
<Click
OnEvent="Button2_Click"
Success="function(config, params) {
Ext.Msg.alert('Warning', params.userParamsResponse.failure);
}"
/>
</AjaxEvents>
</ext:Button>

Hope this helps.

Timothy
Sep 09, 2008, 9:06 PM
Awesome! You already thought about it ;)

Those are MUCH appreciated examples!

Vladimir
Sep 10, 2008, 7:44 AM
Timothy,

one more information. If request was unsuccessful (for example, unhandled exception on server) and ajax event have no failure handler on client then show window with error description (this window showing automatically and can be controled with ShowWarningOnFailure property)



<script runat="server">
protected void Button1_Click(object sender, AjaxEventArgs e)
{
throw new ApplicationException("An Exception was thrown on Button2");
}
</script>

<ext:Button
ID="Button1"
runat="server"
Text="Throw Exception">
<AjaxEvents>
<Click OnEvent="Button1_Click" />
</AjaxEvents>
</ext:Button>


Can you confirm that with this code you see a window after the button click?

Timothy
Sep 10, 2008, 8:37 AM
Yup I do get them, confirmed thanks vlad :)

Cheers,0
Timothy