Question on Displaying Custom Errors

mountainbiking
02-03-2007, 07:06 AM
If I have a DetailsView inside of a FormView (to display relational records) and I want to display a warning to the user that if they don't have the proper session variable value they will be given an error. What I have now is this:

If Session("user") = "" And e.NewMode = DetailsViewMode.Edit Then
Try
Response.Redirect("default.aspx")
Catch ex As Exception
ex.Message.ToString()
End Try
End If
End Sub

This works fine... but I don't really want to be directed to another page. I want to have the error displayed then have the user click a link that send them back to their previous state. Response.Write does not work because it will write my Custom Error then display the rest of the page.

This is how I handled errors in Classic ASP and it worked great. I know there is a new and better method to doing so in ASP.Net I just have not figured it out yet.

P.S. And if I do need to post to a new page (lets just say it would be an errors.aspx page) how could I determine where the error is coming from in order to display the correct message on that page?

Thanks!!!!!

wayneph
02-04-2007, 02:05 PM
One option is to have two panels on your page. One for displaying errors, and one for displaying the rest of your page. If there's an error change the visibility on the panels so that only one is visible at a time.

But you could also take a look into using the customErrors configuration from web.config file, and the Application_OnError event in global.asax. They may be able to help with your Exception handling.

Eduardo Lorenzo
02-04-2007, 06:33 PM
You can use you present logic by redirecting to an error page then place a link button on that page with this code:


reqponse.redirect(request.UrlReferrer.ToString)

mountainbiking
02-05-2007, 06:47 AM
Thanks guys... it seems like the Panel idea would be the best idea in order to control errors and pay layout.

With that being said, how can I actually display a specific error within a panel on the same page....

Here is what I have added thus far:

Protected Sub DetailsView1_ModeChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewModeEventArgs)
If Session("user") = "" And e.NewMode = DetailsViewMode.Edit Then
Try
Panel1.Visible = False
Panel2.Visible = True
Response.Write("Error, you don't have correct permissions")
Catch ex As Exception
ex.Message.ToString()
End Try
End If
End Sub

I am using a Master Page and when I use the response.write it obviously writes the string at the top of the page. What properties of Panel do I need to use in order to submit the error string that I want to display into Panel2?

mountainbiking
02-05-2007, 06:57 AM
Guys,

Don't worry about this one, I figured since it makes the most sense to use Panels in the instance I decided to use a PlaceHolder to hold the value for my error. This is what I came up with and it seems to work great.

Protected Sub DetailsView1_ModeChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewModeEventArgs)
If Session("user") = "" And e.NewMode = DetailsViewMode.Edit Then
Try
Panel1.Visible = False
Panel2.Visible = True
Dim NewLabel As New Label()
NewLabel.Text = "Error, you don't have correct permissions"
PlaceHolder1.Controls.Add(NewLabel)
Catch ex As Exception
ex.Message.ToString()
End Try
End If
End Sub

I think this would work ok for displaying many different custom errors?!?

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum