How to display an error message in an ASP.NET web application

I have an ASP.NET web application, and I wanted to know how I can display an error message box when an exception is thrown.

For instance,

try { do something } catch { messagebox.write("error"); //[This isn't the correct syntax, just what I want to achieve] } 

[An error message is displayed in the message box]

thanks

Duplicate How to display error message box in asp.net c # web application

+4
source share
3 answers

Approximately you can do it as follows:

 try { //do something } catch (Exception ex) { string script = "<script>alert('" + ex.Message + "');</script>"; if (!Page.IsStartupScriptRegistered("myErrorScript")) { Page.ClientScript.RegisterStartupScript("myErrorScript", script); } } 

But I recommend that you define your custom exception and throw it anywhere. On your page, catch this custom exception and register a script message box.

+8
source

Errors in ASP.Net are saved in the Server.GetLastError property,

Or I would put a label on the asp.net page to display the error.

 try { do something } catch (YourException ex) { errorLabel.Text = ex.Message; errorLabel.Visible = true; } 
+2
source

All you need is a control that you can set for text and UpdatePanel if an exception occurs during postback.

If occurs during postback: Markup:

 <ajax:UpdatePanel id="ErrorUpdatePanel" runat="server" UpdateMode="Coditional"> <ContentTemplate> <asp:TextBox id="ErrorTextBox" runat="server" /> </ContentTemplate> </ajax:UpdatePanel> 

code:

 try { do something } catch(YourException ex) { this.ErrorTextBox.Text = ex.Message; this.ErrorUpdatePanel.Update(); } 
+2
source

All Articles