tags:

views:

170

answers:

5

I have my own exception based on some condition and want to raise an alert when control comes in this catch block

catch (ApplicationException ex)
{
    //want to call window.alert function here
}
A: 

Do you mean, a message box?

MessageBox.Show("Error Message", "Error Title", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

More information here: http://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox(v=VS.100).aspx

Ruel
No. The question is about a web app, not a desktop app.
Christian Hayter
On his first undedited question, the tag says, `c#` and `.net`.
Ruel
+1  A: 

I'm not sure if I understand but I'm guessing that you're trying to show a MessageBox from ASP.Net?

If so, this code project article might be helpful: Simple MessageBox functionality in ASP.NET

ho1
+3  A: 

Its a bit hard to give a definate answer without a bit more info, but one usual way is to register a startup script:

try
{
  ...
}
catch(ApplicationException ex){
  Page.ClientScript.RegisterStartupScript(this.GetType(),"ErrorAlert","alert('Some text here - maybe ex.Message');",true);
}
Jamiec
what is "erroralert" in the above solution?
NayeemKhan
@NayeemKhan - They hide that information in the documentation. http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx
Jamiec
+2  A: 

MessageBox like others said, or RegisterClientScriptBlock if you want something more arbitrary, but your use case is extremely dubious. Merely displaying exceptions is not something you want to do in production code - you don't want to expose that detail publicly and you do want to record it with proper logging privately.

annakata
I agree. Redirecting to a fixed custom error page after logging the exception is better practice.
Christian Hayter
A: 

You can use next extension method from any web page or nested user control:

static class Extensions
{
    public static void ShowAlert(this Control control, string message)
    {
        if (!control.Page.ClientScript.IsClientScriptBlockRegistered("PopupScript"))
        {
            var script = String.Format("<script type='text/javascript' language='javascript'>alert('{0}')</script>", message);
            control.Page.ClientScript.RegisterClientScriptBlock(control.Page.GetType(), "PopupScript", script);
        }
    }
}

next way:

class YourPage : Page
{
    private void YourMethod()
    {
        try
        {
            // do stuff
        }
        catch(Exception ex)
        {
            this.ShowAlert(ex.Message);
        }
    }
}
abatishchev
this.showalert() is not available
NayeemKhan
@NayeemKhan: I updated my answer to show how to declare an extension method
abatishchev