Friendly GridView Deletion Messages

Friendly GridView Deletion Messages
by Martin Millar
last updated : 1st September 2006 
Background
you know the scenario. You present a whole load of tabular data to users in a GridView. You may have reduced the font-size so you can cram in an extra couple of rows from the database. You have an Edit and Delete LinkButtons there. So you think well I'll put in a piece of client script to make sure my users get a confirmation pop-up when they are about to delete a record. I'll code the nice OnClientClick event on the LinkButton and put in a 'Are you sure you wish to delete the record' message. 
but wait a minute - my users are not that accurate with the mouse - I'm not that accurate with the mouse. And that font is quite small. How do I make 100% sure that they know exactly what record will be deleted? 
well I'll show you how to include some personal information in your pop-up message so there's no mistakes. 
Strategy
we need to add a Delete LinkButton into a template field in the GridView. When the GridViews RowDataBound event fires for each row we want to set the onClientClick property of the LinkButton with a personalised message 
Implementation
we have a users table that is bound to an ObjectDataSource which in turn is bound onto a GridView control. The GridView code is shown below. 

<asp:GridView ID="GvUsers" runat="server" DataKeyNames="ID" AutoGenerateColumns="False"
    AutoGenerateEditButton="true" DataSourceID="ObjectDataSource1">
  <Columns>
    <asp:TemplateField ShowHeader="False">
    <ItemTemplate>
      <asp:LinkButton ID="DeleteUser" runat="server" CausesValidation="False" CommandName="Delete"
          Text="Delete"></asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="ID" Visible="False" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
    <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
    <asp:BoundField DataField="Telephone" HeaderText="Telephone" SortExpression="Telephone" />
    <asp:BoundField DataField="Hiredate" HeaderText="Hiredate" SortExpression="Hiredate" />
</Columns>
</asp:GridView>
   
if you look at the GridView properties you will see that we have set AutoGenerateEditButton to true but have not auto generated the delete button. We have created a template field and added a LinkButton to it with ID="DeleteUser". Note the CommandName="Delete" which auto-wires the delete button to the GridView. 
next step is to code up the GridViews RowDataBound event. As each row is bound to the GridView we search for the Delete LinkButton and customise the OnClientClick property. We use a simple JavaScript confirm function that returns true if the user clicks OK, otherwise false. 

Protected Sub GvUsers_RowDataBound(ByVal sender As Object, _
              ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _
              Handles GvUsers.RowDataBound
        ' add a confirm button to the delete button
        ' We are only interested in datarows
        If e.Row.RowType = DataControlRowType.DataRow Then
            ' make sure we can find the control first
            If e.Row.FindControl("DeleteUser") IsNot Nothing Then
                ' cast it to a LinkButton and set the client script
                CType(e.Row.FindControl("DeleteUser"), LinkButton).OnClientClick = _
                            "return confirm('Are you sure you want to delete " &;amp; _
                            e.Row.Cells(3).Text.Replace("'", "\'") &;amp; _
                            " from the database?');"
            End If
        End If
End Sub
you can see we are concatenating a display message with a value from the Name field on the GridView. Single quotes cause issues with the JavaScript so we need to escape them by prefixing them with a backslash e.Row.Cells(3).Text.Replace("'", "\'"). This takes care of names like O'Malley etc. 
now when your users hit delete they will receive a much more detailed message like the one below. 


 附件: 您所在的用户组无法下载或查看附件
 
Summary
as you can see this is a very simple technique to add into your web applications. I think this approach is much better than having users on the phone asking for a database restore as they have mistakenly deleted your top customer! 

About the author
martin Millar is an application developer for a large telecoms and internet company based in the UK. He specialises in web development using ASP.NET and SQL Server and is a Microsoft Certified Solutions Developer(MCSD). In his spare time he has started up a fledgling web design company called bracora at
http://www.bracora.com
. You can also see what he's up to in his blog at http://www.martinmillar.co.uk

 感谢原创者的辛勤劳动,希望对您有所帮助,转载请注明原出处。
 您可能对 [Asp.Net] 的这些文章也感兴趣:

ASP.NET用户控件技术(1)
一个完整的支持最小化的自定义Panel控件
为ASP.NET封装的SQL数据库访问类
ASP.NET中Cookie编程的基础知识(5)
技巧:.NET中加密与解密QueryString的方法
Asp.Net上传图片按比例自动缩小或放大
ASP.NET创建文件并写入内容
真正的取真实IP地址及利弊
Asp.Net 2.0之SqlCacheDependency
ASP.NET MVC 源码更新预览