GridView行编辑、更新、取消、删除事件使用方法

GridView行编辑、更新、取消、删除事件使用方法

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

 

注意:当启用编辑button时,点击编辑button后会使一整行都切换成文本框。为了是一行中的一部分是文本框,须要把以整行的全部列都转换成模板,然后删掉编辑模板中的代码。这样就能使你想编辑的列转换成文本框。

1.界面

<asp:GridView ID=”GridView1″ runat=”server” CellPadding=”4″ ForeColor=”#333333″
            GridLines=”None” AutoGenerateColumns=”False”  DataKeyNames=”ProductID”
            onrowdatabound=”GridView1_RowDataBound” AllowPaging=”True”
            onpageindexchanging=”GridView1_PageIndexChanging”
            onrowcommand=”GridView1_RowCommand”
            onrowcancelingedit=”GridView1_RowCancelingEdit”
            onrowediting=”GridView1_RowEditing” onrowupdating=”GridView1_RowUpdating”
            onrowdeleting=”GridView1_RowDeleting”>
            <PagerSettings FirstPageText=”首页” LastPageText=”尾页”
                Mode=”NextPreviousFirstLast” NextPageText=”下一页” PreviousPageText=”上一页” />
            <RowStyle BackColor=”#E3EAEB” />
            <Columns>
                <asp:TemplateField HeaderText=”ProductID”>
 
                    <ItemTemplate>
                        <asp:Label ID=”Label2″ runat=”server” Text='<%# Bind(“ProductID”) %>’></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText=”ProductName”>
                    <EditItemTemplate>
                        <asp:TextBox ID=”txtName” runat=”server” Text='<%# Bind(“ProductName”) %>’></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID=”Label1″ runat=”server” Text='<%# Bind(“ProductName”) %>’></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText=”UnitPrice”>
                    <ItemTemplate>
                        <asp:Label ID=”Label3″ runat=”server” Text='<%# Bind(“UnitPrice”) %>’></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText=”操”>

                    <ItemTemplate>
                        <asp:LinkButton ID=”del” runat=”server” OnClientClick=”return confirm(‘您确定要删除吗?’)” CommandName=”dell” >删除</asp:LinkButton>
                      
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ShowEditButton=”True” HeaderText=”作”>
               
                    <HeaderStyle  HorizontalAlign=”Center” />
                        <ItemStyle  HorizontalAlign=”Center”  Width=”85px” ForeColor=”Blue” />
                    </asp:CommandField>
            </Columns>
            <FooterStyle BackColor=”#1C5E55″ Font-Bold=”True” ForeColor=”White”
                HorizontalAlign=”Right” />
            <PagerStyle BackColor=”#666666″ ForeColor=”White” HorizontalAlign=”Center” />
            <SelectedRowStyle BackColor=”#C5BBAF” Font-Bold=”True” ForeColor=”#333333″ />
            <HeaderStyle BackColor=”#1C5E55″ Font-Bold=”True” ForeColor=”White” />
            <EditRowStyle BackColor=”#7C6F57″ />
            <AlternatingRowStyle BackColor=”White” />
        </asp:GridView>

 

2.前台操控调用业务

DalBll db = new DalBll();
    static List<Products> tmpList = new List<Products>();
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if(!IsPostBack)
        {
            InitGridView();
        }
    }

    private void InitGridView()
    {
        tmpList = db.GetDataList();
        this.GridView1.DataSource = tmpList;
        this.GridView1.DataBind();
    }

    //绑定数据时触发
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Products tmp = e.Row.DataItem as Products;
            LinkButton lbtn = e.Row.FindControl(“del”) as LinkButton;
            if (lbtn != null && tmp != null)
                lbtn.CommandArgument = tmp.ProductID.ToString();//绑定主键
        }
    }

    //删除数据
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == “dell”)
        {
            int productID = Convert.ToInt32(e.CommandArgument);
            db.Del(productID);
        }

    }

    //分页
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        InitGridView();
    }

    //切换到编辑模式
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        InitGridView();
    }

    //取消
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        InitGridView();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //获取当前页索引
        int i = this.GridView1.EditIndex;
        //获取文本框的值
        string productsName = ((TextBox)(this.GridView1.Rows[i].FindControl(“txtName”))).Text.ToString();
        DataKey key = this.GridView1.DataKeys[e.RowIndex];
        int id = Convert.ToInt32(key[0].ToString());
        //首先找到该对象
        Products tmp = tmpList.Where(c => c.ProductID == id).First();
        tmp.ProductName = productsName;
        db.Update(tmp);
        InitGridView();
    }

3.各操作数据的代码

public class DalBll
    {
        NorthwindEntities db = new NorthwindEntities();

        public List<Products> GetDataList()
        {
            return db.Products.ToList();
        }

        public void Del(int productID)
        {
            Products tmp = db.Products.Where(c=>c.ProductID==productID).First();
            db.DeleteObject(tmp);
            db.SaveChanges();
        }

        public void Update(Products tmp)
        {
            //为參数对象创建实体键
            EntityKey key;
            object originalProductObj;
            //因參数对象不属于上下文,因此为该參数对象创建上下文实体键
            key = db.CreateEntityKey(“Products”, tmp);
            db.TryGetObjectByKey(key, out originalProductObj);
            db.ApplyPropertyChanges(key.EntitySetName, tmp);
            db.SaveChanges();
           
        }

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/118540.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Databus简介「建议收藏」

    Databus简介「建议收藏」1.背景在互联网架构中,数据系统通常分为真实数据(source-of-truth)系统,作为基础数据库,存储用户产生的写操作;以及衍生数据库或索引,提供读取和其他复杂查询操作。后者常常衍生自主数据存储,会对其中的数据做转换,有时还要包括复杂的业务逻辑处理。缓存中的数据也来自主数据存储,当主数据存储发生变化,缓存中的数据就需要刷新,或是转为无效。这样架构自然而然的一个问题就是如何保障基础数

    2022年10月17日
    4
  • 卷积核(kernels)与滤波器(filters)的关系「建议收藏」

    卷积核(kernels)与滤波器(filters)的关系「建议收藏」简单理解:卷积核:二维的矩阵滤波器:多个卷积核组成的三维矩阵,多出的一维是通道。先介绍一些术语:layers(层)、channels(通道)、featuremaps(特征图),filters(滤波器),kernels(卷积核)。从层次结构的角度来看,层和滤波器的概念处于同一水平,而通道和卷积核在下一级结构中。通道和特征图是同一个事情。一层可以有多个通道(或者说特征图)。如果输入的是一个R…

    2022年5月21日
    35
  • JavaScript RegExp对象

    JavaScript RegExp对象

    2021年12月14日
    41
  • 使用git stash命令保存和恢复进度[通俗易懂]

    使用git stash命令保存和恢复进度[通俗易懂]使用git stash命令保存和恢复进度

    2022年4月24日
    57
  • Esp8266学习之旅① 搭建开发环境,开始一个“hellow world”串口打印。

    Esp8266学习之旅① 搭建开发环境,开始一个“hellow world”串口打印。本系列博客学习由非官方人员半颗心脏潜心所力所写,仅仅做个人技术交流分享,不做任何商业用途。如有不对之处,请留言,本人及时更改。1、Esp8266之搭建开发环境,开始一个“hellowworld”串口打印。2、Esp8266之利用GPIO开始使用按钮点亮你的“第一盏灯”。3、Esp8266之利用“软件定时器”定时0.5秒闪烁点亮一盏LED。4、Esp8266之了解P

    2022年5月30日
    55
  • ffprobe参数详解(基于版本4.0.1)

    ffprobe参数详解(基于版本4.0.1)Simplemultimediastreamsanalyzerusage:ffprobe[OPTIONS][INPUT_FILE]Mainoptions:-L         showlicense-htopic      showhelp-?topic      showhelp-helptopic    …

    2025年6月6日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号