Tuesday, November 29, 2005

GridView CommandField Validation woes

Today I was working with a simple form, a single textbox at the top with a button for adding items, and a gridview below for editing/deleting.

With 2.0’s validation groups, we can have validation on the “Add” form by setting validationgroup=’AddGroup’ and we can have our CommandField column of the GridView have validationGroup=’editDelete’.  Ideally this will separate the 2 actions into their own validation groups and they won’t bother each other.

Well, if you are using ImageButtons instead of LinkButtons for your CommandField, there is a slight problem.  The ImageButton actually gets instantiated as DataControlImageButton, which cannot use the CausesValidation property.  Here’s what happens:

DumbException

Now why does this generate a NotSupportedException?  This is a perfectly valid scenario for validation?  In this particular case I do not care about the edit/delete validation, but I do care about the Add validation. 

If I change this scenario to work with LinkButton or just Plain Button, it all works out fine.

I have another rant regarding this same concept.  The above screenclip is showing the RowDataBound event handler, and I am looping through the controls in the last column of the GridView.  Now the last column is going to be the CommandField, so I know that there will be some commandcontrols in there.  At this point I should not care whether it is a Button, an LinkButton, or an ImageButton because I will be treating them all the same:  I will compare their CommandName properties to see what they are doing.  So why can’t I just cast the control as Button?  Well ImageButton doesn’t even derive from Button!  I am sure there are reasons for that, but I don’t see them.  This means I have to recompile my code if some UI guy decides they want a link instead of an image for their Edit command.  Dumb, I say.  An ImageButton, by nature, is a button, not an image.  It’s behavior is a button.  The way we treat it as developers is just like a button.  It’s view or representation however is an Image, just like a standard button has a raised, rounded rectange as its view.  Someone please explain to me why I’m wrong….

Now playing: Jimi Hendrix - Red House

Tuesday, November 29, 2005 4:31:18 PM (Central Standard Time, UTC-06:00)
ASP.NET 2.0 > Me
Tuesday, November 29, 2005 5:10:17 PM (Central Standard Time, UTC-06:00)
It's really cool, but all of the neato tricks that they show are only skin deep. If you want any deep customization you still have to do it by hand. I am happy with the language improvments though.
Comments are closed.
Secured Loan - Loans - New York Hotels - Homeowner Loans