It would really be much easier just to validate the data before the error using the same logic that others have presented in a Try/Catch block:
Try
If Not IsNumeric(txtCompPS.Text) Then
Throw New Exception("Only numbers are allowed for PS quantity.")
End If
Catch
Session("Alert") = ex.Message
Finally
If Len(Session("Alert")) = 0 Then
Session("Confirm") = "[Any confirmation message you want]."
End If
End Try
Then on the front page I have:
<%@ Register TagPrefix="include" TagName="Alert" src="../include/alert.ascx" %>
<%@ Register TagPrefix="include" TagName="Confirm" src="../include/confirm.ascx" %>
before the <head></head> tags.
And in the body:
<include:Alert ID="Alert" Runat="server" />
<include:Confirm ID="Confirm" Runat="server" />
Alert page:
<%@ Control Language="
vb" AutoEventWireup="false" Codebehind="alert.ascx.
vb" Inherits="extranet.include_alert" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<table cellpadding="0" cellspacing="5" class="alert" id="tblAlert" runat="server">
<tr>
<td class="alert">
<img border="0" src="../images/icons/alert.gif" width="16" height="16" /><br />
</td>
<td width="100%" class="alert">
<asp:Literal ID="litAlert" Runat="server" /><br />
</td>
</tr>
<tr id="tdrOptions" runat="server">
<td class="alert_options">
<br />
</td>
<td class="alert_options">
<asp:Literal ID="litOptions" Runat="server" /><br />
</td>
</tr>
</table>
Alert Code behind:
Inherits System.Web.UI.UserControl
Private _Alert As String
Private _Options As String
Protected tblAlert As HtmlTable
Protected litAlert As Literal
Protected tdrOptions As HtmlTableRow
Protected litOptions As Literal
Private Sub Page_Prerender(ByVal Sender As Object, ByVal e As EventArgs) Handles MyBase.PreRender
_Alert = Session("Alert")
_Options = Session("Options")
If Len(_Alert) = 0 Then
tblAlert.Visible = False
Else
tblAlert.Visible = True
litAlert.Text = _Alert
If Len(_Options) = 0 Then
tdrOptions.Visible = False
Else
tdrOptions.Visible = True
litOptions.Text = _Options
End If
Session("Alert") = ""
Session("Options") = ""
End If
End Sub