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


相关推荐

  • LoadRunner 详细使用教程

    LoadRunner 详细使用教程打开VirtualUserGenerator(虚拟用户生成器)打开后会有一个小弹窗,点击closeNewScriptandSolution(新建脚本和解决方案)创建脚本选择SingleProtocol下面的Web-HTTP/HTML,在写脚本名称、选择存放位置、解决方案、打钩最后点击Create(创建)就可以了创建成功后点解决方案下Test下面的Action,点击菜单栏的Record里的Record或者点红圈中的红点录制脚本(..

    2022年5月24日
    38
  • java中JSONArray、JSONObject、List、String之间的转换「建议收藏」

    java中JSONArray、JSONObject、List、String之间的转换「建议收藏」一、JASSONArray转为JSONObject     JSONArrayresult_type=newJSONArray();       StringBuffercdsIdxType=newStringBuffer();       cdsIdxType.append("selectidfromtable_type");       result_type=…

    2022年6月3日
    37
  • Docker创建MySQL集装箱

    Docker创建MySQL集装箱

    2022年1月5日
    46
  • 【软件工程】——详细设计说明书「建议收藏」

    【软件工程】——详细设计说明书「建议收藏」1引言1.1编写目的该文档在概要设计的基础上,进一步的细化系统结构,展示了软件结构的图标,物理设计、数据结构设计、及算法设计、详细的介绍了系统各个模块是如何实现的,包括涉及到的算法,逻辑流程等。预期的读者:程序员1.2背景a. 待开发软件系统的名称:机房收费系统b. 项目的任务提出者:米新江教授c. 项目的开发者:齐智d. 项目的用户:廊坊师范学院全体在职员工及学生e. 运行该软…

    2022年6月12日
    32
  • 数据库置疑处理_sqlserver可疑数据库恢复

    数据库置疑处理_sqlserver可疑数据库恢复现象说明:新备份出的数据库Geb,在还原时报错”MicrosoftSQL-DMO(ODBCSQLState:42000)” 解决方法: 分离出还原失败的数据库Geb 先创建一个同样的数据库Geb 停掉server服务,用旧的数据文件覆盖新创建的文件(只要mdf就可以)。  启动server服务  运行以下命令  sp_configur

    2022年8月22日
    3
  • LayUI树形表格treetable使用详解[通俗易懂]

    LayUI树形表格treetable使用详解[通俗易懂]LayUI是现在比较流行的一款前端框架,也有很多人基于LayUI开发了很多不错的组件,比如treetable树形表格。因为treetable是第三方基于LayUI开发的,所以需要先用Layui引入一下文件。layui.config({base:’static/layui/’}).extend({treetable:’treetable-lay/treetab…

    2022年6月13日
    299

发表回复

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

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