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

ASP.Net Cheats!


I've been "programming" web sites with MS ASP.Net 2.0 for a couple of years now.

Not coming from a programming background, it has been hard going at times to make the magic happen. One of the best resources available to me has been the good ol' power of Google.

A lot of the time though, the answers I needed were written by much more knowledgeable people than me who used terms I didn't understand, or they glossed over some important supporting parts that a newbie like me couldn't get my head around. As a result, I would search for hours for solutions to ASP.Net problems, hitting the back button a lot of the time because the answers went straight over my head.

Through perseverance though, in most cases I eventually found that one, golden answer that stops a looooong forum dead in its tracks, because it is THE answer!

These are the answers I will share with you in upcoming posts to hopefully save you hours of trawling through irrelevant or "my 2 cents" forum posts!

Feel free to run any of your little niggles past me on larry @ digitalsick.com incase I've come across them before.

I can highly recommend starting off learning from some good books, such as SAMS Teach yourself ASP.Net in 24 hours.