2

My web forms inherits a class called MyPageMain, which inhertis System.Web.UI.Page and is located on the App_Code folder.

In this MyPageMain class I created this method, to record and output Exceptions (error messages):

public string Error(Exception pException, string pFriendlyMessage)
{
    using (BusError erro = new BusError())
    {
        int? errorId = //HERE Routine to log the error;

        StringWriter stringWriter = new StringWriter();

        using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
        {

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "erroMain");
            writer.RenderBeginTag(HtmlTextWriterTag.Div); //<div>

            writer.RenderBeginTag(HtmlTextWriterTag.P); //<p>
            writer.Write(pFriendlyMessage);
            writer.RenderEndTag(); // </p>
            writer.RenderBeginTag(HtmlTextWriterTag.Small); 
            writer.Write("Event tracker:");
            writer.Write(errorId);
            writer.RenderEndTag(); 

            writer.RenderEndTag(); // </div>
            Console.WriteLine(stringWriter.ToString());
        }

    }
}

Then, when there is some exception on the page, I call it, like this:

    Protected void btnLoad_Click(object sender, EventArgs e)
    {
         LoadData();
    }        

    private void LoadData()
    {
        try
        {
             //Loading some data here.
        }
        catch (Exception ex)
        {
             Error(ex, "Unexpected error trying to load data.");
        }
    }

This is bulding OK, but doesn't work... I think that one of the reasons may be the fact that the whole thing is inside an UpdatePanel. Any suggestions on how to make this work?

Is this Console.Writeline suitable for what i'm trying to do? Is this going to work on UpdatePanels?

Already Tried with Response.Write(...) and it does work. But not inside an UpdatePanel

8
  • I can't exactly understand what but doesn't work... means Reis.Did you debug it?Does it enter LoadData()?Does it go to Error(..)?Does it hit Console.WriteLine(stringWriter.ToString()); line?Did you see the error message in the child page?Did you try it without an UpdatePanel?
    – Bastardo
    Jul 20, 2011 at 19:09
  • Yes, I Did Debugged it. It enters LoadData(), When an exception occurs, it goes to Error() and hit Console.WriteLine(..). But nothing happens.. What I'm trying to learn is if this approach can work with this Console.WriteLine
    – Vitor Reis
    Jul 20, 2011 at 19:52
  • Did you solve this problem Reis?
    – Bastardo
    Jul 21, 2011 at 9:24
  • Sorry for not comming back... too busy. Didn't solved this yet
    – Vitor Reis
    Jul 29, 2011 at 6:25
  • np, I want to open a bounty for this question if it is still not solved Reis.
    – Bastardo
    Aug 2, 2011 at 6:50

8 Answers 8

3
+50

When you use an UpdatePanel, you can only update content within the panel during an async postback triggered from that panel, so your error message will have to appear somewhere within the UpdatePanel. (That is, unless you set UpdateMode="Always" on one of your UpdatePanels, then its content is updated on every async and full postback. But that doesn't help you here unless you put your error message in its own UpdatePanel with UpdateMode="Always", which would require you to add said UpdatePanel to every page. I understand this is not what you want to do.)

The following example will work to add the error message at the top of the UpdatePanel.

You will need to add a Control errorParent parameter to your Error method, so it can add the error message as child control to that parent control.

In your catch block, just pass in whatever container control where you want the error message to appear. That control must be a container to accept child controls, so it has to be something that renders as a <div> or <span> tag, like an asp:Panel or asp:UpdatePanel.

In the example below, you could use errorParent.Controls.Add(errorControl) to show the error message at the bottom of the UpdatePanel, or use AddAt() with a different index. Just be sure that index will always work on every page.

Take a parent control and add a new Literal child control:

public string Error(Exception pException, string pFriendlyMessage, Control errorParent)
{
    using (BusError erro = new BusError())
    {
        int? errorId = //HERE Routine to log the error;

        Literal errorControl = new Literal();
        errorControl.Text = String.Format("<div class=\"errorMain\"><p>{0}</p><small>Event Tracker: {1}</small></div>", pFriendlyMessage, errorId);
        errorParent.Controls.AddAt(0, errorControl);
    }
}

Pass in the parent control:

private void LoadData()
{
    try
    {
         //Loading some data here.
    }
    catch (Exception ex)
    {
         Error(ex, "Unexpected error trying to load data.", MyUpdatePanel.ContentTemplateContainer);
    }
}
2

Since your Error method returns string, you can return error message and display it.

Place Literal in your UpdatePanel (maybe in MasterPage, so you do not have to write it for all 40 or more pages). When exception is thrown, handle it with your Error method and set returned message to Literal.

Console.WriteLine is not usable in ASP.NET. Use Debug.WriteLine instead. It will write to Output window in VisualStudio.

2

Many of the answers are dancing around the issue. Console.WriteLine is a command used to output a line of text for a Console application (command-line). This is a Web app. So, you either use Response.Write to spit a string out to the Response stream or set the text of a Literal control that is already on the page and set it to be visible (default hidden).

Since I myself completely missed the UpdatePanel detail, here's an alternate solution to make up for it. My example uses a hidden div and some jQuery through the usage of the ScriptManager control to inject text into a div that is outside the UpdatePanel. Based on the conditions of the go_Click handler method of the button, it'll show or hide an error message using jQuery that gets injected into the Page server-side at the time of the UpdatePanel's update.

Note that it is critically important to use the ScriptManager's functions to register JavaScript rather than Page.ClientScript when using AJAX. Using the latter won't add the JS to the page.

Page markup

<div id="errorMessagePlaceHolder" class="error" style="display:none;">
</div>
<asp:UpdatePanel ID="myPanel" runat="server">
    <ContentTemplate>
        <asp:TextBox ID="myText" runat="server" />
        <asp:Button ID="go" Text="GO" runat="server" OnClick="go_Click" />
    </ContentTemplate>
</asp:UpdatePanel>

Page code-behind

public partial class _Default : Page {
    protected void go_Click(object sender, EventArgs e) {
        try {
            // Do something that can throw an exception

            // Hide the error if we reach the end as we may have triggered
            // it in a prior update and no longer want it to display.
            HideError();
        } catch (Exception) {
            ShowError("myText is empty!");
        }
    }

    #region Move to a base page or a helper class if it helps reduce duplication

    private void HideError() {
        ScriptManager.RegisterStartupScript(Page, GetType(), "HideErrorMessageScript", HideErrorMessageScript(), true);
    }

    private void ShowError(string errorMessage) {
        ScriptManager.RegisterStartupScript(Page, GetType(), "ErrorMessageScript", ErrorMessageScript(errorMessage), true);
    }

    private static string HideErrorMessageScript() {
        return @"
$(document).ready(function() {
    $('#errorMessagePlaceHolder').hide();
});
";
    }

    private static string ErrorMessageScript(string errorMessage) {
        return
            string.Format(
                @"
$(document).ready(function() {{
    $('#errorMessagePlaceHolder').html('{0}');
    $('#errorMessagePlaceHolder').show();
}});
",
                errorMessage);
    }

    #endregion

}
2

I feel like nobody has actually answered the entire question yet, so let me try.

Is this Console.Writeline suitable for what i'm trying to do?

Probably not, unless you just want debug information to appear in the output window while debugging. Some people like to keep outputting info to the Console, even in a release build and so if you want to do that with your errors, it's a valid choice, but you should also be showing a friendly error message to the user and/or logging the error to some kind of persistence (eg. log file). All of the answers above are suggesting ways for you to show some notification of the error to the user.

Is this going to work on UpdatePanels?

There is NO reason why it shouldn't if your triggers and the like are set up properly (and we know they are because you said that you're able to hit the Console.WriteLine breakpoint). I wonder if your Console output is working fine but you're looking for it in the wrong place? In the typical configuration for a web application, it should appear in the Output window of the Visual Studio instance you're using to Debug.


As to a suggestion for what I think you're asking, which is "how do I get my error message to appear in the update panel if there's an error loading the content for that update panel?"...

I would suggest an approach similar to nekno's proposal above. You can either dynamically add a control whose text you can set to be the error string during update panel request processing or you can even have an initially hidden/collapsed control on the page, which is only set to be visible in the case of an error. I'd prefer the latter approach because it's more maintainable. You can completely control the look and feel of the error label from the markup/display views of your ASPX page.

1
  • 1
    I would agree that having a target place holder of some kind would be the best --- and typical --- design. Ordinarily there should be a server control to receive new content you want to display, rather than trying to shoe-horn new content into your DOM. On the other hand, there's nothing fundamentally wrong with adding a server control from your code-behind. I can understand why he doesn't want to have to update dozens of existing pages. Hopefully, most of the display and styling will be controlled by his errorMain CSS class, and not hard-coded in compiled code.
    – nekno
    Aug 10, 2011 at 15:56
1

Are you calling .update() on your UpdatePanel after your LoadData() method runs? I think the UpdatePanel won't automatically fire unless the postback originates from inside it.

Ex:

Protected void btnLoad_Click(object sender, EventArgs e)
{
    LoadData();
    MyUpdatePanel.Update(); 
} 

Update: I was able to get Response.Write() to work in an UpdatePanel by registering a trigger like this <Triggers><asp:PostBackTrigger ControlID="btn1" /></Triggers> for the button that will create the error.

6
  • The UpdatePanel is updating normally.. All data is displayed, But when there's an exception, the Error method won't write anything
    – Vitor Reis
    Jul 20, 2011 at 19:13
  • You have two different writers working on in your Error() method. I'm not seeing where your displaying the contents of HtmlTextWriter. Is there more code to this?
    – Chris
    Jul 20, 2011 at 19:57
  • HtmlTextWriter uses stringWriter as an output. All data is in the stringWriter object
    – Vitor Reis
    Jul 20, 2011 at 20:09
  • Console.WriteLine() is not the correct way to go about this. You could simply use a hidden <div> element on your page and when an error occurs you'll populate it with your error message and then set it to visible at the end of the error routine.
    – Chris
    Jul 20, 2011 at 20:16
  • 1
    I want this method to be generic to all of my 40 pages. not to create a div on each page..
    – Vitor Reis
    Jul 20, 2011 at 20:31
1

Are you trying to log errors or display them? If you are trying to display them, you should:

On your masterpage, create a spot where your errors will be displayed (maybe below your navigation bar or near the bottom:

<asp:ContentPlaceHolder id='cphError'></asp:ContentPlaceHolder>

Everytime you get an error, you can call your error function as is, but instead of writing out the text with Response.Write() or Console.WriteLine(), you can add a label in there:

Label lError = new Label();
lError.CssClass = '..';
lError.Text = '..';
cphError.Controls.Add(lError);

This will help you out with displaying the error, but AJAX will still be a problem. At this point, you have two options:

  1. Since update panels update other update panels, you can wrap an update panel around cphError, this way your AJAX calls will still update that area

  2. OR: You can can use ClientScript.RegisterStartupScript to create the error label. (This method is only preferable if you do not have a static error message spot)

1
  • According to his comment on @Chris's answer, @Vitor doesn't want to add something to every page, and from that I'm also assuming he isn't using master pages. 1. That's only true if you have UpdateMode="Always" set. That's extra data for every postback. 2. That requires manually inserting elements into the DOM in script. It would be easier just to add the controls to the UpdatePanel from the server side and let the PageRequestManager update the client DOM. Also, when using an UpdatePanel one should use ScriptManager.RegisterStartupScript().
    – nekno
    Aug 9, 2011 at 21:42
1

Place a Panel on the master, extend it with the modalpopupextender in the AjaxToolKit. When the error is raised, write the contents of string writer to a label in the panel E.g. Label lbl = (Label)Master.FindControl() lbl.text = StringWriter.ToString();

// Now access the modal popup AjaxControlToolkit.ModalPopupExtender MPE = (AjaxControlToolkit.ModalPopupExtender)Master.findControl(""); // and show it MPE.Show();

The Modal will need a hidden button to use as it's target control, but it will not require a user's input. I accomplish this on the page with the following code:

            <asp:Button ID="btnDummyOK" runat="server" Style="display: none;" />
        <asp:ModalPopupExtender ID="mpePnlErrors" runat="server" TargetControlID="btnDummyButton" PopupControlID="pnlErrirs" BackgroundCssClass="modalBackground" CancelControlID="btnCancel" OkControlID="btnOK" />

Good luck

0

I think Console.WriteLine don''t work on asp.net application and i think inside an update panel you can simple fire the throw New Exception( the friendly error message) and it will appear in an alert box.

1
  • First part (Console.WriteLine doesn't work in ASP.NET) is right, second part (throw new Exception automagically makes a UI alert appear to the web user) is wrong.
    – wonkim00
    Aug 8, 2011 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.