Groups | Blog | Home
all groups > asp.net building controls > may 2005 >

asp.net building controls : IPostBackDataHandler and validating a control -- the best place to do this.


Martin
5/17/2005 12:00:00 AM
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
5/17/2005 2:53:38 PM
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
www.PeterBlum.com
Email: PLBlum@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

[quoted text, click to view]

Martin
5/18/2005 12:00:00 AM
Hi Peter,

Thanks for all of the information, i found it really usefull.
I have one additional question about your forth option

[quoted text, click to view]

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.




[quoted text, click to view]

AddThis Social Bookmark Button