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


相关推荐

  • noip2018提高组初赛解析_NOIP提高组

    noip2018提高组初赛解析_NOIP提高组题目背景NOIP2012提高组DAY2试题。题目描述H国有n个城市,这n个城市用n-1条双向道路相互连通构成一棵树,1号城市是首都,也是树中的根节点。H国的首|都爆发了一种危害性极高的传染病。当局为了控制疫情,不让疫情扩散到边境城市(叶子节点所表示的城市),决定动用军队在一些城市建立检查点,使得从首都到边境城市的每一条路径上都至少有一个检查点,边境城市也可以…

    2022年8月22日
  • CentOS 7 x8安装记录

    CentOS 7 x8安装记录CentOS7x8安装插入刻录好的U盘(刻录这里我就不说了,有实用的刻录链接可以下方留言给我我补充上)按下拥有弹性的开机按钮,开启你的装机旅程,启动时注意!按F11进入BootMenu一、在BootMenu中选择UEFI:VendorCoProductCode,Partition1如果选择了VendorCoProductCode安装结束后,启动系统后就会下面这副惨样子…

    2022年5月25日
  • vim退出快捷键「建议收藏」

    vim退出快捷键「建议收藏」退出vim的快捷键,不需要进入命令编辑模式按住shiftzz  保存退出zq  不保存退出,q表示放弃之所以按住shift,其实是切换大小写 在命令编辑模式下::q不保存退出:q!不保存强制退出:wq保存退出,w表示写入,不论是否修改,都会更改时间戳:x  保存退出,如果内容未改,不会更改时间戳

    2022年8月24日
  • LOAM 论文及原理分析「建议收藏」

    LOAM 论文及原理分析「建议收藏」前言:由于对三维激光SLAM比较感兴趣,并且最近也在找无人驾驶激光SLAM算法的岗位,所以花了一个多月把LOAM的论文和源码好好看了一遍。发现论文还是比较容易明白,但一看代码全是坑。看论文懂了,看代码似懂非懂。为了尽快把这坑填上,所以诚邀读者一起探讨。作者始终认为填坑最好的方法是拉别人和你一起填坑。由于三千多行的源码不是一篇博客能够讲明白的,所以这篇博客主要讲一下我对LOAM论文…

    2022年5月11日
  • python字典dict方法_python中dict的用法

    python字典dict方法_python中dict的用法文章目录:一.字典(dict)的概念:二.字典(dict)的定义:1.一般格式:2.空字典:3.举例:注意:三.字典(dict)的一些基本操作:1.增:2.删:3.查:4.改:四.字典(dict)的常见操作:五.字典三种取值方式:1.value=字典名[key]:2.setdefault:3.get:六.字典的遍历:1.key:2.value:3.item:4.依次打印key和value:5.元素值和对应的下标索引(enumerate()):一.字典(dict)的概念:Python字典是另一种可变容器模

    2022年10月6日
  • HDFS常用命令的学习

    HDFS常用命令的学习HDFS常用命令的学习文章目录HDFS常用命令的学习1.hadoopfs,hadoopdfs,hdfsfs,hdfsdfs区别2.Hadoop命令3.HDFS常用命令命令的使用常用命令示例小结hdfs与getconf结合使用hdfs与dfsadmin结合使用hdfs与fsck结合使用其他命令4.安全模式1.hadoopfs,hadoopdfs,hdfsfs,hdfsdfs区别fs与dfs有什么区别呢?fs是文件系统,dfs是分布式文件系统。fs

    2022年10月3日

发表回复

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

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