Professional Web Applications Themes

IPostBackDataHandler and validating a control -- the best place to do this. - ASP.NET Building Controls

Hi, I have an asp.net page with two composite controls on it as well as a single button in the asp.net page AND a single dropdown box on the page that has its autopostback property set to true so that it posts back when ever the selection is changed. what i would like to do is when the button (in the aspx page NOT on any of the composite controls) is clicked and causes a postback to validate both composite controls - however i want no validation to occur if the dropdown box causes the postback. I have implemented IPostBackDataHandler ...

  1. #1

    Default IPostBackDataHandler and validating a control -- the best place to do this.

    Hi,

    I have an asp.net page with two composite controls on it as well as a single
    button in the asp.net page AND a single dropdown box on the page that has
    its autopostback property set to true so that it posts back when ever the
    selection is changed.

    what i would like to do is when the button (in the aspx page NOT on any of
    the composite controls) is clicked and causes a postback to validate both
    composite controls - however i want no validation to occur if the dropdown
    box causes the postback.

    I have implemented IPostBackDataHandler in both controls and have verified
    that they are both notified when the button in the page is clicked.
    so i figured that i could validate each control in its respective
    "RaisePostDataChangedEvent"

    so basically i need to work out how to know exactly which element on the
    page caused a postback.

    I therefore have two questions.

    1. Is my method of validating each composite control on a page in it's
    respective "RaisePostDataChangedEvent" the correct way of achieving such a
    feet or do I need to re-think my stagegy, if so any advice is welcome..

    2. How do I tell exactly tell which element on a page caused a postback.

    I appreciate any feedback or pointers to article of interest.

    thanks in advance.

    cheers

    martin.


    Martin Guest

  2. #2

    Default Re: IPostBackDataHandler and validating a control -- the best place to do this.

    Do not manually validate insider your custom control. Always let the page
    developer identify exactly when validation occurs.
    1. Buttons automatically call the Page.Validate() method for you unless
    their CausesValidation property is false. This happens in their OnClick
    method after Page_Load is done an before calling your Click event method.
    Page.Validate() will run your validators.

    2. Sometimes a button should never validate, such as a Cancel button. Again
    the user takes control by setting CausesValidation to false.

    3. AutoPostBack never calls Page.Validate(). If the user elects to validate
    on autopostback, they add a call to Page.Validate() in their own event
    handler for that post back.

    4. Users can also call the Validate() method on individual validators and
    check their IsValid property to see the result. If you want to support this,
    I recommend adding a method to your custom control called Validate() that
    fires the Validate() methods on each validator. Also include an IsValid
    property which returns true if all validators are valid.

    --- Peter Blum
    [url]www.PeterBlum.com[/url]
    Email: [email]PLBlumPeterBlum.com[/email]
    Creator of "Professional Validation And More" at
    [url]http://www.peterblum.com/vam/home.aspx[/url]

    "Martin" <martin_no_spammartinz.co.nz> wrote in message
    news:OYgcT%23qWFHA.132TK2MSFTNGP14.phx.gbl...
    > Hi,
    >
    > I have an asp.net page with two composite controls on it as well as a
    > single
    > button in the asp.net page AND a single dropdown box on the page that has
    > its autopostback property set to true so that it posts back when ever the
    > selection is changed.
    >
    > what i would like to do is when the button (in the aspx page NOT on any of
    > the composite controls) is clicked and causes a postback to validate both
    > composite controls - however i want no validation to occur if the dropdown
    > box causes the postback.
    >
    > I have implemented IPostBackDataHandler in both controls and have verified
    > that they are both notified when the button in the page is clicked.
    > so i figured that i could validate each control in its respective
    > "RaisePostDataChangedEvent"
    >
    > so basically i need to work out how to know exactly which element on the
    > page caused a postback.
    >
    > I therefore have two questions.
    >
    > 1. Is my method of validating each composite control on a page in it's
    > respective "RaisePostDataChangedEvent" the correct way of achieving such a
    > feet or do I need to re-think my stagegy, if so any advice is welcome..
    >
    > 2. How do I tell exactly tell which element on a page caused a postback.
    >
    > I appreciate any feedback or pointers to article of interest.
    >
    > thanks in advance.
    >
    > cheers
    >
    > martin.
    >
    >

    Peter Blum Guest

  3. #3

    Default Re: IPostBackDataHandler and validating a control -- the best place to do this.

    Hi Peter,

    Thanks for all of the information, i found it really usefull.
    I have one additional question about your forth option
    > 4. Users can also call the Validate() method on individual validators and
    > check their IsValid property to see the result. If you want to support
    this,
    > I recommend adding a method to your custom control called Validate() that
    > fires the Validate() methods on each validator. Also include an IsValid
    > property which returns true if all validators are valid.
    Ok, so I add a method called validate to my custom control, but how can I
    ensure that this gets fired.
    for example the "Validate()" method of an asp page gets fired after
    page_load has run on postback but before any button click where the button
    has it causesvalidation property set to true.
    How can i implement this behaviour in a composite server controls??

    can you offer any advice??

    cheers

    martin.




    "Peter Blum" <PLBlumBlum.info> wrote in message
    news:uSgN$GxWFHA.1148tk2msftngp13.phx.gbl...
    > Do not manually validate insider your custom control. Always let the page
    > developer identify exactly when validation occurs.
    > 1. Buttons automatically call the Page.Validate() method for you unless
    > their CausesValidation property is false. This happens in their OnClick
    > method after Page_Load is done an before calling your Click event method.
    > Page.Validate() will run your validators.
    >
    > 2. Sometimes a button should never validate, such as a Cancel button.
    Again
    > the user takes control by setting CausesValidation to false.
    >
    > 3. AutoPostBack never calls Page.Validate(). If the user elects to
    validate
    > on autopostback, they add a call to Page.Validate() in their own event
    > handler for that post back.
    >
    >
    > --- Peter Blum
    > [url]www.PeterBlum.com[/url]
    > Email: [email]PLBlumPeterBlum.com[/email]
    > Creator of "Professional Validation And More" at
    > [url]http://www.peterblum.com/vam/home.aspx[/url]
    >
    > "Martin" <martin_no_spammartinz.co.nz> wrote in message
    > news:OYgcT%23qWFHA.132TK2MSFTNGP14.phx.gbl...
    > > Hi,
    > >
    > > I have an asp.net page with two composite controls on it as well as a
    > > single
    > > button in the asp.net page AND a single dropdown box on the page that
    has
    > > its autopostback property set to true so that it posts back when ever
    the
    > > selection is changed.
    > >
    > > what i would like to do is when the button (in the aspx page NOT on any
    of
    > > the composite controls) is clicked and causes a postback to validate
    both
    > > composite controls - however i want no validation to occur if the
    dropdown
    > > box causes the postback.
    > >
    > > I have implemented IPostBackDataHandler in both controls and have
    verified
    > > that they are both notified when the button in the page is clicked.
    > > so i figured that i could validate each control in its respective
    > > "RaisePostDataChangedEvent"
    > >
    > > so basically i need to work out how to know exactly which element on the
    > > page caused a postback.
    > >
    > > I therefore have two questions.
    > >
    > > 1. Is my method of validating each composite control on a page in it's
    > > respective "RaisePostDataChangedEvent" the correct way of achieving such
    a
    > > feet or do I need to re-think my stagegy, if so any advice is welcome..
    > >
    > > 2. How do I tell exactly tell which element on a page caused a postback.
    > >
    > > I appreciate any feedback or pointers to article of interest.
    > >
    > > thanks in advance.
    > >
    > > cheers
    > >
    > > martin.
    > >
    > >
    >
    >

    Martin Guest

Similar Threads

  1. validating datagridtextboxcolumn textbox, validating event fire twice, why?
    By SMai24@gmail.com in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: September 27th, 07:10 PM
  2. Implementing IPostBackDataHandler in a custom web control with VB.NET
    By Andrew Dawson in forum ASP.NET Building Controls
    Replies: 1
    Last Post: August 12th, 06:34 PM
  3. validating a custom control in a datagrid
    By philipl@vistatec.ie in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: June 8th, 12:21 PM
  4. Validating a custom, composite web control
    By Mark Rodseth in forum ASP.NET Building Controls
    Replies: 0
    Last Post: August 11th, 04:38 PM
  5. validating a control created at runtime
    By David Foster in forum ASP.NET General
    Replies: 0
    Last Post: July 23rd, 01:10 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139