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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java json序列化日期类型[通俗易懂]

    java json序列化日期类型[通俗易懂]做接口开发时经常需要给前端返回日期数据,比如生日、创建时间、更新时间等。我们一般是建一个bean,将定义所需要的字段,并和数据库的字段相对应。虽然数据库的字段是日期类型的,但bean的字段定义在String就行了,看下面的测试代码:packagecom.bs.test;importjava.text.SimpleDateFormat;importjava.util.Date;importc

    2025年6月9日
    5
  • 机顶盒知识详解_罗盘的知识与技巧

    机顶盒知识详解_罗盘的知识与技巧机顶盒定义数字视频变换盒(英语:SetTopBox,简称STB),通常称作机顶盒或机上盒,是一个连接电视机与外部信号源的设备;它可以将压缩的数字信号转成电视内容,并在电视机上显示出来;信号可以来自有线电缆、卫星天线、宽带网络以及地面广播。机顶盒接收的内容除了模拟电视可以提供的图像、声音之外,更在于能够接收数据内容,包括电子节目指南、因特网网页、字幕等等;使用户能在现有电视机上观…

    2025年8月6日
    4
  • 配置管理小报100127:端口

    配置管理小报100127:端口

    2021年8月25日
    71
  • OpenSSL密码库算法笔记——第5.2章 椭圆曲线算法的函数架构图

    OpenSSL密码库算法笔记——第5.2章 椭圆曲线算法的函数架构图椭圆曲线算法中涉及的函数纷繁复杂,比如为了实现“复制点群”功能,就定义了四个函数,有:intEC_GROUP_copy(EC_GROUP*dest,constEC_GROUP*src)、intec_GFp_mont_group_copy(EC_GROUP*dest,constEC_GROUP*src)、intec_GFp_simple_group_copy(…

    2022年7月20日
    13
  • Python 万能代码模版:爬虫代码篇「建议收藏」

    Python 万能代码模版:爬虫代码篇「建议收藏」你好,我是悦创。很多同学一听到Python或编程语言,可能条件反射就会觉得“很难”。但今天的Python课程是个例外,因为今天讲的**Python技能,不需要你懂计算机原理,也不需要你理解复杂的编程模式。**即使是非开发人员,只要替换链接、文件,就可以轻松完成。并且这些几个实用技巧,简直是Python日常帮手的最佳实践。比如:爬取文档,爬表格,爬学习资料;玩转图表,生成数据可视化;批量命名文件,实现自动化办公;批量搞图,加水印、调尺寸。接下来,我们就逐一用Python实

    2022年5月26日
    80
  • ubuntu安装nginx及其依赖「建议收藏」

    ubuntu安装nginx及其依赖「建议收藏」最近安装nginx踩了一些坑,所以在这记录一下,也希望能够帮助到一些人。下面就开始说一下我的安装步骤:一:安装pcre:打开终端,输入以下命令:sudo-s#进入root用户aptinstallbuild-essential#安装gcc编译器及其环境,包含gcc,gdb,make等wgethttps://ftp.pcre.org/pub/pcre/pcre-8.37.tar.gz#下载安装包tar-zxvfpcre-8.37.tar.gz#解压cd

    2026年1月25日
    4

发表回复

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

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