.Net Tips and Tricks

Blog archive

Adding Custom Functionality to ASP.NET DataViews

The ASP.NET DataViews, when connected to a DataSource, generate all the buttons a user needs to edit, update and delete the data that the DataView displays (assuming that you enable edits, updates and deletes).

But what if you want to add some additional functionality to the form? If, for instance, you want the user to be able to click a button to run a credit check on the currently displayed customer? Or, for some change that requires coordinating changes to several values, you want to give the user the ability to implement the update with a single button click? Or, by clicking a button, delete not the currently displayed record but some other, related set of records (i.e. "Delete all orders for this customer")? You can put that code where it belongs -- associated with the DataView -- rather than scattered through several Click events by using the FormView's ItemCommand method.

All you have to do is drop a Button of your own onto the form and set the Button's CommandName property to some string. When the user clicks on any Button on the DataView, the DataView fires its ItemCommand event. The e parameter to that event has a property called CommandName that's automatically set to whatever was in the CommandName property of the button that triggered the event. You can use that value to check to see which Button was clicked and execute your code. Typical ItemCommand code will look like this:

 Select Case e.CommandArgument
Case "CreditCheck"
'....
Case "DeleteAllOrders"
'....
'CommandArguments for other buttons
End Select

This event has two other properties useful in this scenario. The CommandArgument property contains the value of the CommandArgument property on the Button the user clicked. You can store data in the CommandArgument that will be passed to the ItemCommand event when the Button is clicked. Less useful is the CommandSource, which points at the Button that fired the event (the sender parameter passed to the event points to the DataView).

And, because the ItemCommand event fires for every Button on the page and fires before any of the events triggered by that button, the ItemCommand event lets you preview all other Button clicks on the page. This allows you to use the ItemCommand event as a central point-of-control for every Button on the DataView.  

Posted by Peter Vogel on 10/24/2011 at 1:48 PM


comments powered by Disqus

Reader Comments:

Tue, Nov 22, 2011 Peter Vogel Canada

Daves: Sorry I didn't see this earlier! I'll see if I can put together a column. I think that the answer you're looking for is to use the ASP.NET Ajax Cascading DropDown Extender. The problem with using the standard controls is that you have to postback to the server to run your server-side code and fill your lists. As soon as you postback, your DataView loses its mind. But it's possible that by using the Ajax extender, that problem can be avoided.

Tue, Nov 15, 2011 DaveS WV

how about example of how to have a large number of combo/drop that are dependent on each other in a dataview. Example you have a customer that then display multiple location, that then display multiple warehouses. That unique customer/location/warehouse then would have only available a unique product/type/size. Each customer that you view would be totally unique from the next, and must exist in the database. No typing of data allowed that is not in the database that fit's the unique cust/loc/wareH. You then have to have a database that has a customer file, location file, warehouse file and product file tied to the orders file. These kind of problems I never can use the form view or dataview or gridview. I always seem to have to do a custom page with text box's and loading and unloading of the data. Thanks for listening to my grip about these short cut tools.

Add Your Comments Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above