GridView里添加自定义LinkButton,编定自定义动作

前台文件:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Debug="true"%>

<!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 id="Head1" runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
   
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="0" ForeColor="#333333" GridLines="None" Width="100%" AllowPaging="True" AllowSorting="True" OnRowCommand="Re_GridView1_ItemCommand" onrowcancelingedit="GridView1_RowCancelingEdit" onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
<Columns>
                <asp:TemplateField HeaderText="Id">
                    <EditItemTemplate>
                        &nbsp;<asp:Label ID="Adolescent_Id" runat="server" Text='<%# Eval("Growup_User_Adolescent_Id") %>'></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Growup_User_Adolescent_Id") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                </asp:TemplateField>
                <asp:BoundField DataField="Growup_User_Adolescent_Name" HeaderText="姓名" >
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                </asp:BoundField>
                <asp:BoundField DataField="Growup_User_Adolescent_Account" HeaderText="帐号" >
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                </asp:BoundField>
                <asp:BoundField DataField="Growup_User_Adolescent_Phone" HeaderText="联系电话" >
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                </asp:BoundField>
                <asp:BoundField DataField="Growup_User_Adolescent_Adress" HeaderText="地址" >
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                </asp:BoundField>
                <asp:TemplateField HeaderText="加入时间">
                    <EditItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("Growup_User_Adolescent_Date", "{0:d}") %>'></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Growup_User_Adolescent_Date", "{0:d}") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                </asp:TemplateField>
    <asp:TemplateField HeaderText="编辑 " ShowHeader="False">
        <EditItemTemplate>
            <asp:LinkButton ID="LinkButton_Update" runat="server" CausesValidation="True" CommandName="Update"  Text="更新"></asp:LinkButton>
            <asp:LinkButton ID="LinkButton_Cancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消"></asp:LinkButton>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:LinkButton ID="LinkButton_Edit" runat="server" CausesValidation="False" CommandName="Edit" Text="编辑"></asp:LinkButton>
            <asp:LinkButton ID="LinkButton_Delete" runat="server" CausesValidation="False" CommandName="Delete" Text="删除" OnClientClick="return confirm('你确定要删除吗?')"></asp:LinkButton>
            <asp:LinkButton ID="LinkButton_Rest_Wor" runat="server" CommandArgument='<%# Eval("Growup_User_Adolescent_Id") %>' CausesValidation="False" CommandName="Rest_Word" Text="重置密码" OnClientClick="return confirm('密码将被重置为123456,确定重置?')"></asp:LinkButton>
        </ItemTemplate>
        <HeaderStyle HorizontalAlign="Left" />
        <ItemStyle HorizontalAlign="Left" />
    </asp:TemplateField>
            </Columns>
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        </asp:GridView>

    </div>
    </form>
</body>
</html>

 

 

后台文件:

using System;
using System.Data;
using System.Data.SqlClient;
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 _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }

    private void Bind()
    {
        GridView1.DataSource = SqlHelper.ExecuteDateSet(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, "select * from [Growup_User_Adolescent] order by Growup_User_Adolescent_Date", null);
        GridView1.DataKeyNames = new String[] { "Growup_User_Adolescent_Id" };
        GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)//编辑操作
    {
        GridView1.EditIndex = e.NewEditIndex;
        //Response.Write(e.NewEditIndex);
        Bind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)//删除操作
    {
        try
        {
            SqlParameter[] param = new SqlParameter[5];
            param[0] = new SqlParameter("@Growup_User_Adolescent_Id", SqlDbType.Int);
            param[0].Value = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());

            if (SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, "delete  from [Growup_User_Adolescent] where Growup_User_Adolescent_ID=@Growup_User_Adolescent_Id", param) > 0)
            {
                Response.Write("<script>alert('删除成功!');</script>");
                Bind();
            }
            else
            {
                Response.Write("<script>alert('系统出错,请联系网站制作人-玲珑 QQ41611039!');</script>");
            }
        }
        catch
        {
            Response.Write("<script>alert('系统出错,请联系网站制作人-玲珑 QQ41611039!');</script>");
        }

    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)//更新操作
    {
        try
        {
            SqlParameter[] param = new SqlParameter[5];
            param[0] = new SqlParameter("@Growup_User_Adolescent_Id", SqlDbType.Int);
            param[0].Value = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
            param[1] = new SqlParameter("@Growup_User_Adolescent_Name", SqlDbType.VarChar, 50);
            param[1].Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString().Trim();
            param[2] = new SqlParameter("@Growup_User_Adolescent_Account", SqlDbType.VarChar, 50);
            param[2].Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString().Trim();
            param[3] = new SqlParameter("@Growup_User_Adolescent_Phone", SqlDbType.VarChar, 50);
            param[3].Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString().Trim();
            param[4] = new SqlParameter("@Growup_User_Adolescent_Adress", SqlDbType.VarChar, 150);
            param[4].Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString().Trim();

            if (SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, "update [Growup_User_Adolescent] set Growup_User_Adolescent_Name=@Growup_User_Adolescent_Name , Growup_User_Adolescent_Account=@Growup_User_Adolescent_Account , Growup_User_Adolescent_Phone=@Growup_User_Adolescent_Phone , Growup_User_Adolescent_Adress=@Growup_User_Adolescent_Adress where Growup_User_Adolescent_Id=@Growup_User_Adolescent_Id", param) > 0)
            {
                //Response.Write("<script>alert('成功!');location.href='Growup_Article_Cla_Adolescent.aspx';</script>");
                GridView1.EditIndex = -1;
                Bind();
            }
            else
            {
                Response.Write("<script>alert('系统出错,请联系网站制作人-玲珑 QQ41611039!');</script>");
            }
        }
        catch
        {
            Response.Write("<script>alert('系统出错,请联系网站制作人-玲珑 QQ41611039!');</script>");
        }


    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)//取消操作
    {
        GridView1.EditIndex = -1;
        Bind();
    }


    protected void Re_GridView1_ItemCommand(object source, System.Web.UI.WebControls.GridViewCommandEventArgs e)//重置密码操作
    {
        if (e.CommandName == "Rest_Word")
        {
            try
            {
                SqlParameter[] param = new SqlParameter[2];
                param[0] = new SqlParameter("@Growup_User_Adolescent_Id", SqlDbType.Int);
                param[0].Value = Convert.ToInt32((e.CommandArgument).ToString());
                param[1] = new SqlParameter("@Growup_User_Adolescent_Word", SqlDbType.VarChar, 50);
                param[1].Value = Regular.Md5("123456");

                if (SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, "update [Growup_User_Adolescent] set Growup_User_Adolescent_Word=@Growup_User_Adolescent_Word where Growup_User_Adolescent_Id=@Growup_User_Adolescent_Id", param) > 0)
                {
                    Response.Write("<script>alert('成功!');</script>");
                    //GridView1.EditIndex = -1;
                    Bind();
                }
                else
                {
                    Response.Write("<script>alert('系统出错,请联系网站制作人-玲珑 QQ41611039!');</script>");
                }
            }
            catch
            {
                Response.Write("<script>alert('系统出错,请联系网站制作人-玲珑 QQ41611039!');</script>");
            }
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值