Solved

Call Control from master page using OnError method

Posted on 2007-11-16
7
1,594 Views
Last Modified: 2013-12-17
I am using VS 2005. I have a web page designed using Ajax and ASP.NET , VB.Net is code behind.
In my master page I have a panel that visible property is set to false. In this panel i have a message I want to display when an error occurs in my application.
E.G. List.aspx page - To catch the error i have a method

Protected Overrides Sub OnError(ByVal e As System.EventArgs)
        MyBase.OnError(e)

        '    'do stuff here
        UpdatePanel1.Visible = False

        CType(Me.Form.Parent.FindControl("ErrorHandlerPanel"), Panel).Visible = True ' This is the panel on my master page
       
        Response.Write(Server.GetLastError().Message)

        Server.ClearError()

    End Sub

To test this in my application I have added Throw New ApplicationException("Blahh") on my page load event.

All thats displayed is "Blahh" . Would anyone know how to display my panel from my master page?

However, if I remove Throw New ApplicationException("Blahh") and add CType(Me.Form.Parent.FindControl("ErrorHandlerPanel"), Panel).Visible = True into the onLoad event, it will display fine.
It just seems that the problem is getting the panel to display from the onError method.

Any ideas?
0
Comment
Question by:caoimhincryan
  • 3
  • 3
7 Comments
 
LVL 6

Expert Comment

by:ventaur
ID: 20298797
You cannot call Response.Write in your method. Doing so will usually wipe out your controls and mess up the rendering of the page.

If you'd like to show the message inside your panel, try this instead. You could even put this in the master page instead.
Protected Overrides Sub OnError(ByVal e As System.EventArgs)

  MyBase.OnError(e)

  

  ...
 

  ' Get the error panel from the master page.

  Dim ErrorPanel As Panel = CType(Me.Form.Parent.FindControl("ErrorHandlerPanel"), Panel)

  

  ' Add the error message to the panel and show it.

  Dim ErrorMessage As New LiteralControl(Server.GetLastError().Message)

  ErrorPanel.Controls.Clear()

  ErrorPanel.Controls.Add(ErrorMessage)

  ErrorPanel.Visible = True
 

  Server.ClearError()

End Sub

Open in new window

0
 

Author Comment

by:caoimhincryan
ID: 20299027
Hi Ventaur,

Thanks for the quick response.

However all that is displaying is a blank screen.

Any ideas?
0
 
LVL 6

Accepted Solution

by:
ventaur earned 500 total points
ID: 20299331
I see now. That's apparently by design, because the exception stops code execution, which prevents controls from being rendered (Page and MasterPage included).

Your best bet will be to use custom errors and create a default error page. You can set that up in your web.config file. Otherwise, I think you may be out of luck with your design.
<customErrors mode="On" defaultRedirect="ErrorPage.aspx" />

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:caoimhincryan
ID: 20299418
I agree with you ventaur. I will leave the question open another while to see if anyone has other ideas for the question.
0
 
LVL 6

Expert Comment

by:ventaur
ID: 20299444
Understood. Good luck!
0
 
LVL 33

Expert Comment

by:raterus
ID: 20299784
Are your users going to debug and fix your application?  They why display the error message to them?  I personally email errors to the programmers, and redirect the user to a basic page saying "whoops".
0
 

Author Comment

by:caoimhincryan
ID: 20311169
I thought maybe with Ajax that it might be possible instead of an error page. Error page is the way to go.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now