Each time a data record is added to the Repeater control, an ItemDataBound event is fired. Within the event, you can access the controls that are created, as well as the data being bound to the row. This feature enables you to do a variety of things, such as change the data going in, add attributes to controls, etc. This example adds a JavaScript confirmation to the Delete LinkButton control.

Each item will generate an ItemDataBound event, but you will also get events for other types of items, including the header and footer. As a result, you need to protect your code by checking the ItemType before trying to do any work. The following code adds the confirmation dialog:

This code looks for Item or AlternatingItem rows, finds the btnDelete control using the FindControl method, and then adds the onclick attribute to the Attributes collection. The result is that when the user clicks the Delete LinkButton, an OK/Cancel dialog pops up to confirm the delete. This is a handy way to protect the user from doing something dangerous.

About the Author

Eric Smith is the owner of Northstar Computer Systems, a Web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic.

Please enable Javascript in your browser, before you post the comment! Now Javascript is disabled.