Sunday, 3 February 2008

Easily Confirm Delete on Gridview

Gridview's are awesome. You can do in 2 minutes what would take you an hour in classic ASP.

If you're making a system for other people to use though, you might be concerned at the ease at which they could accidentally delete items.

Easily fixed! It's simple to get an "ok", "cancel" message box to pop up when you press delete...

  • 1) Convert the "CommandField" into a template field (Click on "Edit Columns" on the "Smart Tag" at the top right of your datagrid, highlight the field in the list on the bottom right then click the blue "Convert to template field" hyperlink at the bottom right)

  • 2) Go to the source view to look at the page markup and find the code containing commandname="Delete"
  • 3) add the following within the tag...

OnClientClick="return confirm('Message to user'); "

So the whole piece of code looks like this...

< asp:linkbutton id="LinkButton3" text="Delete" causesvalidation="False" onclientclick="return confirm('Do you really wish to remove this Term Completely? It cannot be recovered once deleted!');" commandname="Delete" runat="server" > < /asp:linkbutton>

Done! Give it a try...here's the result when you press "Delete" on the gridview...







Please leave a comment if it helped you out!

Larry

0 comments: