说明:近来看到不少人想在DataGrid、GridView 使用比较灵活的方式来编辑、查看、删除其中的数据选项(当然它们本身也内置了这些功能),却苦于找不到比较好的办法,本人结合实际代码说明一下如何使用LinkButton来实现上述功能。
 
--------------------------------------------GridViewLinkButton.aspx-----------------------------------------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewLinkButton.aspx.cs" Inherits="GridViewLinkButton" %>
<!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="gvPersonList" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="Id" HeaderText="编号" />
                <asp:BoundField DataField="Name" HeaderText="姓名" />
                <asp:TemplateField HeaderText="性别">
                    <ItemTemplate>
                        <%# Eval("Sex").ToString()=="true"?"男":"女" %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Age" HeaderText="年龄" />
                <asp:TemplateField HeaderText="婚否">
                    <ItemTemplate>
                        <%# Boolean.Parse(Eval("Married").ToString())==true?"是":"否" %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="婚否">
                    <ItemTemplate>
                        <asp:LinkButton CommandArgument='<%# Eval("Id") %>' Enabled='<%# Boolean.Parse(Eval("Married").ToString())==true?false:true %>' Text="设为结婚" runat="server" OnCommand="SetMarried"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
   
    </div>
    </form>
</body>
</html>
--------------------------------------GridViewLinkButton.aspx.cs------------------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class GridViewLinkButton : System.Web.UI.Page
{
    private string firstName = "赵钱孙李周吴郑王冯陈诸卫蒋沈韩杨朱秦尤许何吕施张孔曹严华";
    private string lastName="猛勇刚强豹彪雁燕蓉菲";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindGridView();
        }
    }
    private void BindGridView()
    {
        DataTable myData = CreateDataTable();
        Session["MyData"] = myData;
        gvPersonList.DataSource = myData;
        gvPersonList.DataBind();
    }
    //手动生成DataTable
    private DataTable CreateDataTable()
    {
        DataTable data = new DataTable();
        DataColumn dcId = new DataColumn("ID", typeof(Int32));
        dcId.AutoIncrement = true;
        dcId.AutoIncrementSeed = 1;
        dcId.AutoIncrementStep = 1;
        data.Columns.Add(dcId);
        data.Columns.Add(new DataColumn("Name",typeof(string)));
        data.Columns.Add(new DataColumn("Age",typeof(int)));
        data.Columns.Add(new DataColumn("Sex",typeof(bool)));
        data.Columns.Add(new DataColumn("Married",typeof(bool)));
        DataRow dataRow = null;
        Random random = new Random();
        for (int i = 0; i < 20; i++)
        {
            dataRow = data.NewRow();
            //随机生成姓名
            dataRow["Name"] = firstName.Substring(random.Next(firstName.Length), 1) + lastName.Substring(random.Next(lastName.Length), 1);
            //随即生成介于20至100之间的年龄
            int age = random.Next(20, 100);
            dataRow["Age"] = age;
            //随即设置性别
            bool sex = (random.Next(100) % 2 == 0) ? true : false;
            dataRow["Sex"] = sex;
            if (((sex == true) && (age >= 22)) || ((sex == false) && (age >= 20)))//男性结婚年龄大于22周岁,女性结婚年龄大于20周岁
            {
                dataRow["Married"] = (random.Next(500) % 2 == 0) ? true : false;
            }
            else
            {
                dataRow["Married"] = false;
            }
            data.Rows.Add(dataRow);
        }
        return data;
    }
    //LinkButton的OnCommand事件的处理方法
    public void SetMarried(Object sender, CommandEventArgs e)
    {
        e.CommandArgument.ToString();
        DataTable data = Session["MyData"] as DataTable;
        for (int i = 0; i < data.Rows.Count; i++)
        {
            if (data.Rows[i]["Id"].ToString() == e.CommandArgument.ToString())
            {
                data.Rows[i]["Married"] = true;
                break;
            }
        }
        gvPersonList.DataSource = data;
        gvPersonList.DataBind();
    }
}
-----------------------------------运行结果-----------------------------------------------------------------
可以看见,一旦某个人是已婚,则不能再设置为已婚状态了。
下图是将某些未婚人士设置成已婚后的截图。