Handling validation errors on the page level

Discussion in 'ASP .Net' started by hardieca@hotmail.com, Jul 30, 2007.

  1. Guest

    Hi,

    I've created an n-tier app where validation rules reside in the
    business layer. When a webform is saved, a business object examines
    its state, and if some property is invalid, throws a custom exception
    (ValidationException)

    The exception bubbles up to the UI where it gets trapped:

    Private Sub Page_Error(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles MyBase.Error
    Dim ctx As HttpContext = HttpContext.Current

    Dim exception As Exception = ctx.Server.GetLastError()

    If TypeOf (exception) Is ValidationException Then
    Me.txtErrorMessages.Text = exception.message
    ctx.Server.ClearError()
    End If
    End Sub

    The exception is being successfully caught, but at the end of the
    process I'm served with an empty page. What I would like to have
    happen is the page reloads with all the form's information in addition
    to a textbox being populated with the exception's message. I tried
    using response.redirect(), but of course the textbox does not get
    populated.

    I'd rather not redirect the user to an error page, I want them to be
    able to fix the validation errors on the form and continue on from
    there. What's the best way to tackle this?

    Regards,

    Chris
     
    , Jul 30, 2007
    #1
    1. Advertising

  2. bruce barker Guest

    just create a custom validator and add to page.

    CustomValidator cv = new CustomValidator();
    cv.IsValid = false;
    cv.ErrorMessage = "my error";
    cv.Visible = true;
    cv.Display = ValidatorDisplay.None;
    this.Form.Control.Add(cv);


    -- bruce (sqlwork.com)

    wrote:
    > Hi,
    >
    > I've created an n-tier app where validation rules reside in the
    > business layer. When a webform is saved, a business object examines
    > its state, and if some property is invalid, throws a custom exception
    > (ValidationException)
    >
    > The exception bubbles up to the UI where it gets trapped:
    >
    > Private Sub Page_Error(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles MyBase.Error
    > Dim ctx As HttpContext = HttpContext.Current
    >
    > Dim exception As Exception = ctx.Server.GetLastError()
    >
    > If TypeOf (exception) Is ValidationException Then
    > Me.txtErrorMessages.Text = exception.message
    > ctx.Server.ClearError()
    > End If
    > End Sub
    >
    > The exception is being successfully caught, but at the end of the
    > process I'm served with an empty page. What I would like to have
    > happen is the page reloads with all the form's information in addition
    > to a textbox being populated with the exception's message. I tried
    > using response.redirect(), but of course the textbox does not get
    > populated.
    >
    > I'd rather not redirect the user to an error page, I want them to be
    > able to fix the validation errors on the form and continue on from
    > there. What's the best way to tackle this?
    >
    > Regards,
    >
    > Chris
    >
     
    bruce barker, Jul 30, 2007
    #2
    1. Advertising

  3. Guest

    Thanks for the response, but I'm still getting served an empty page.
    My handler now looks like this:

    Private Sub Page_Error(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles MyBase.Error
    Dim ctx As HttpContext = HttpContext.Current

    Dim exception As Exception = ctx.Server.GetLastError()

    If TypeOf (exception) Is ApplicationException Then
    Dim cv As New CustomValidator()
    cv.IsValid = False
    cv.ID = "myCV"
    cv.ErrorMessage = Exception.Message
    cv.Visible = True
    cv.Display = ValidatorDisplay.None
    Me.Form.Controls.Add(cv)
    End If

    ' --------------------------------------------------
    ' To let the page finish running we clear the error
    ' --------------------------------------------------
    ctx.Server.ClearError()

    End Sub

    I would really like to figure out a way to validate and preserve the
    form information in the Page_Error handler because I intend to have
    the handler inherited by every page that has a form on it. I would
    rather have this validation check performed in the Page_Error of the
    base page class all my pages inherit from rather than have a Try/Catch
    block on every page. Is there any way to do this?

    Chris

    On Jul 30, 12:18 pm, bruce barker <> wrote:
    > just create a custom validator and add to page.
    >
    > CustomValidator cv = new CustomValidator();
    > cv.IsValid = false;
    > cv.ErrorMessage = "my error";
    > cv.Visible = true;
    > cv.Display = ValidatorDisplay.None;
    > this.Form.Control.Add(cv);
    >
    > -- bruce (sqlwork.com)
    >
    > wrote:
    > > Hi,

    >
    > > I've created an n-tier app where validation rules reside in the
    > > business layer. When a webform is saved, a business object examines
    > > its state, and if some property is invalid, throws a custom exception
    > > (ValidationException)

    >
    > > The exception bubbles up to the UI where it gets trapped:

    >
    > > Private Sub Page_Error(ByVal sender As Object, ByVal e As
    > > System.EventArgs) Handles MyBase.Error
    > > Dim ctx As HttpContext = HttpContext.Current

    >
    > > Dim exception As Exception = ctx.Server.GetLastError()

    >
    > > If TypeOf (exception) Is ValidationException Then
    > > Me.txtErrorMessages.Text = exception.message
    > > ctx.Server.ClearError()
    > > End If
    > > End Sub

    >
    > > The exception is being successfully caught, but at the end of the
    > > process I'm served with an empty page. What I would like to have
    > > happen is the page reloads with all the form's information in addition
    > > to a textbox being populated with the exception's message. I tried
    > > using response.redirect(), but of course the textbox does not get
    > > populated.

    >
    > > I'd rather not redirect the user to an error page, I want them to be
    > > able to fix the validation errors on the form and continue on from
    > > there. What's the best way to tackle this?

    >
    > > Regards,

    >
    > > Chris
     
    , Jul 30, 2007
    #3
  4. bruce barker Guest

    if the Page Error event fires, page processing is cancelled. you are
    supposed to redirect to the error page of your choice. clearing the
    errors does not restart page processing.

    you should be catching your BI validation errors in the calling method,
    not at the page level.

    -- bruce (sqlwork.com)

    wrote:
    > Thanks for the response, but I'm still getting served an empty page.
    > My handler now looks like this:
    >
    > Private Sub Page_Error(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles MyBase.Error
    > Dim ctx As HttpContext = HttpContext.Current
    >
    > Dim exception As Exception = ctx.Server.GetLastError()
    >
    > If TypeOf (exception) Is ApplicationException Then
    > Dim cv As New CustomValidator()
    > cv.IsValid = False
    > cv.ID = "myCV"
    > cv.ErrorMessage = Exception.Message
    > cv.Visible = True
    > cv.Display = ValidatorDisplay.None
    > Me.Form.Controls.Add(cv)
    > End If
    >
    > ' --------------------------------------------------
    > ' To let the page finish running we clear the error
    > ' --------------------------------------------------
    > ctx.Server.ClearError()
    >
    > End Sub
    >
    > I would really like to figure out a way to validate and preserve the
    > form information in the Page_Error handler because I intend to have
    > the handler inherited by every page that has a form on it. I would
    > rather have this validation check performed in the Page_Error of the
    > base page class all my pages inherit from rather than have a Try/Catch
    > block on every page. Is there any way to do this?
    >
    > Chris
    >
    > On Jul 30, 12:18 pm, bruce barker <> wrote:
    >> just create a custom validator and add to page.
    >>
    >> CustomValidator cv = new CustomValidator();
    >> cv.IsValid = false;
    >> cv.ErrorMessage = "my error";
    >> cv.Visible = true;
    >> cv.Display = ValidatorDisplay.None;
    >> this.Form.Control.Add(cv);
    >>
    >> -- bruce (sqlwork.com)
    >>
    >> wrote:
    >>> Hi,
    >>> I've created an n-tier app where validation rules reside in the
    >>> business layer. When a webform is saved, a business object examines
    >>> its state, and if some property is invalid, throws a custom exception
    >>> (ValidationException)
    >>> The exception bubbles up to the UI where it gets trapped:
    >>> Private Sub Page_Error(ByVal sender As Object, ByVal e As
    >>> System.EventArgs) Handles MyBase.Error
    >>> Dim ctx As HttpContext = HttpContext.Current
    >>> Dim exception As Exception = ctx.Server.GetLastError()
    >>> If TypeOf (exception) Is ValidationException Then
    >>> Me.txtErrorMessages.Text = exception.message
    >>> ctx.Server.ClearError()
    >>> End If
    >>> End Sub
    >>> The exception is being successfully caught, but at the end of the
    >>> process I'm served with an empty page. What I would like to have
    >>> happen is the page reloads with all the form's information in addition
    >>> to a textbox being populated with the exception's message. I tried
    >>> using response.redirect(), but of course the textbox does not get
    >>> populated.
    >>> I'd rather not redirect the user to an error page, I want them to be
    >>> able to fix the validation errors on the form and continue on from
    >>> there. What's the best way to tackle this?
    >>> Regards,
    >>> Chris

    >
    >
     
    bruce barker, Jul 30, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    964
    Mark Goldin
    Jan 17, 2004
  2. Replies:
    2
    Views:
    445
  3. =?Utf-8?B?Sm9l?=

    page-level vs control-level enableViewState

    =?Utf-8?B?Sm9l?=, Oct 26, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    2,932
    S. Justin Gengo
    Oct 26, 2005
  4. Keith Patrick

    Page level errors not getting caught

    Keith Patrick, Nov 16, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    343
    Keith Patrick
    Nov 17, 2005
  5. pabbu
    Replies:
    8
    Views:
    732
    Marc Boyer
    Nov 7, 2005
Loading...

Share This Page