[CLOSED] AjaxEvents and Exception

  1. #1

    [CLOSED] AjaxEvents and Exception

    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
  2. #2

    RE: [CLOSED] AjaxEvents and Exception

    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.

    Geoffrey McGill
    Founder
  3. #3

    RE: [CLOSED] AjaxEvents and Exception

    Awesome! You already thought about it ;)

    Those are MUCH appreciated examples!
  4. #4

    RE: [CLOSED] AjaxEvents and Exception

    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?

  5. #5

    RE: [CLOSED] AjaxEvents and Exception

    Yup I do get them, confirmed thanks vlad :)

    Cheers,0
    Timothy

Similar Threads

  1. [CLOSED] TabPanel exception
    By moth1 in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Sep 11, 2011, 11:55 PM
  2. [CLOSED] Set Hidden value after exception
    By asztern in forum 1.x Legacy Premium Help
    Replies: 5
    Last Post: Nov 16, 2010, 5:05 PM
  3. [CLOSED] [1.0] JavaScript Exception?
    By Timothy in forum 1.x Legacy Premium Help
    Replies: 11
    Last Post: Mar 14, 2010, 8:44 PM
  4. [CLOSED] GridPanel Exception
    By Timothy in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Nov 06, 2008, 4:30 PM
  5. [CLOSED] AjaxEvents
    By Timothy in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Sep 10, 2008, 6:37 PM

Posting Permissions