GridView中妙用LinkButton

简介:
说明:近来看到不少人想在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();
    }
}
-----------------------------------运行结果-----------------------------------------------------------------
可以看见,一旦某个人是已婚,则不能再设置为已婚状态了。
下图是将某些未婚人士设置成已婚后的截图。
 
 













本文转自周金桥51CTO博客,原文链接:http://blog.51cto.com/zhoufoxcn/166993  ,如需转载请自行联系原作者


相关文章
|
开发框架 JavaScript .NET
Asp.net 控件用法汇总-RadioButtonList、DropDownList、button、Checkbox...(续)
Asp.net 控件用法汇总-RadioButtonList、DropDownList、button、Checkbox...(续)
136 0
|
存储 C#
C#(三十)之C#comboBox ListView treeView
本篇内容记录了comboBox下拉列表框属性、ListView 列表视图属性、ListView 列表视图事件、treeView树状视图属性。
271 0
C#(三十)之C#comboBox ListView treeView
|
C#
WPF 获取 ListView DataTemplate 中控件值
原文:WPF 获取 ListView DataTemplate 中控件值 版权声明:本文为博主原创文章,未经博主允许可以随意转载 https://blog.csdn.
1687 0
|
C#
WPF中Expander与ListBox(ItemsControl)嵌套中的问题
原文:WPF中Expander与ListBox(ItemsControl)嵌套中的问题 1. 当ListBox放在Expander中时,为了要实现实时更新数据的效果,这里使用了    ObservableCollection类型来作为数据源,         初始的简单例子如下:只有一个List...
1793 0