For Answers, see/post comments

Error Handling

Hi All,
I am running in to a problem with a text box on my page. I have ValidateRequest="true" on my page, which obviously means the page craps out when they enter invalid characters. This is good, but I'd like to trap this error and present a message to the user saying they have entered invalid characters, otherwise I follow my usual error flow and go to the custom error page. Is there a simple way to do this? I am using a method on my code behind which I grabbed somewhere else and the method is being hit. Apologies as I can't remember what site I grabbed this from, but it is only for test purposes:

Protected Overrides Sub OnError(ByVal e As EventArgs)
' At this point we have information about the error
Dim ctx As HttpContext = HttpContext.Current
Dim exception As Exception = ctx.Server.GetLastError
Dim errorInfo As String = ("
Offending URL: "
_
+ (ctx.Request.Url.ToString + ("
Source: "
_
+ (exception.Source + ("
Message: "
_
+ (exception.Message + ("
Stack trace: "
+ exception.StackTrace)))))))
exception.
ctx.Response.Write(errorInfo)
' --------------------------------------------------
' To let the page finish running we clear the error
' --------------------------------------------------

ctx.Server.ClearError()
MyBase.OnError(e)
End Sub

This method gets hit when the error arises and I'm just spitting it out to the page right now. My question is whether or not there is a way to trap the specific error I am looking for and otherwise throw it up to be handled as a regular error. Is there a specific error number I can access? Or maybe I'm way off track and there is a simpler way of handling this. Please let me know any feedback you have.

Thanks

3 comments:

Anonymous said...

why dont you use a RegularExpressionValidator to restrict the users input , and provide approiate message for instance like below which restricts input to

between 1 and 40 alphanumeric characters

<asp:RegularExpressionValidator ControlToValidate="txtYourTextbox" text='Entry not correct format' Display="Dynamic"

ValidationExpression="^[\w]{1,40}$" runat="server"></asp:RegularExpressionValidator>

Anonymous said...

Thanks for the quick reply - sorry I'm a bit tardy on my reply. I am trying to get this to work properly with this multilingual system I am working on. Having a few problems, but I will mark as answer if I can get this to successfully work.

Thanks again.

Anonymous said...

no problem good luck with it