65.9K
CodeProject 正在变化。 阅读更多。
Home

使用模板按钮删除 GridView 中的数据

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.50/5 (5投票s)

2010年9月20日

CPOL
viewsIcon

58377

使用模板按钮删除 GridView 中的数据

本文档片段解释了如何使用模板按钮从 GridView 中删除数据。
现在我们来看如何在 GridView 中添加模板按钮。首先在 GridView 中选择 编辑列

其次,在 GridView 中添加 模板列

现在配置 模板列,点击 编辑模板。在 模板 字段中放置一个 LinkButton。点击 编辑数据绑定,然后选择 CommandArgument,之后,设置字段绑定,绑定到 "ID" 字段,这个 ID 字段用于在服务器端代码中删除数据。然后按下确定按钮。

现在我们可以配置 LinkButton [删除按钮]。
选择 LinkButton 的 属性。将 CommandName 和 Text 设置为 Delete。我们可以在 GridView1_RowCommand 事件中访问这个 CommandName。之后,添加 GridView 事件 GridView1_RowCommandGridView1_RowDeleted

每当点击与 GridView 控件中的项目关联的删除按钮时,都会引发 RowDeleted 事件,但会在 GridView 控件删除记录之后。

这允许您提供一个事件处理方法,以执行自定义例程,例如检查删除操作的结果,每当发生此事件时。为了避免错误,我们在 GridView1_RowDeleted 事件中添加一段自定义代码。

//handling gridview delete excetion
e.ExceptionHandled = true;

现在是服务器端部分。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        //handling gridview delete excetion
        e.ExceptionHandled = true;
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "dele")
        {
            //database Helper
            DBHelper objDBHelper = new DBHelper();
            //sql command to delete data from database
            string sqlCmdText = string.Format
		("DELETE FROM Table WHERE ID='{0}'", e.CommandArgument.ToString());
            //Executeing sql command
            objDBHelper.ExecuteScalar(sqlCmdText);
            //refresh gridview
            GridView1.DataBind();
        }
    }
}

ASPX 页面

<%@ Page Language="C#" AutoEventWireup="true" 
	CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataSourceID="SqlDataSource1" onrowcommand="GridView1_RowCommand"
            onrowdeleted="GridView1_RowDeleted">
            <Columns>
                <asp:BoundField DataField="biInvitationId" HeaderText="biInvitationId"
                    InsertVisible="False" ReadOnly="True" 
				SortExpression="biInvitationId" />
                <asp:BoundField DataField="biEventName" HeaderText="biEventName"
                    SortExpression="biEventName" />
                <asp:BoundField DataField="biHostName" HeaderText="biHostName"
                    SortExpression="biHostName" />
                <asp:BoundField DataField="biTelephone" HeaderText="biTelephone"
                    SortExpression="biTelephone" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server"
                            CommandArgument='<%# Eval("biInvitationId") %>' 
				CommandName="dele">Delete</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server">
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

谢谢!

欢迎评论。

使用模板按钮删除 GridView 中的数据 - CodeProject - 代码之家
© . All rights reserved.