zoukankan      html  css  js  c++  java
  • GridView 编辑功能实现 Delete 、 Update、Edit、Cancel

    GridView 编辑功能实现
    2009年06月18日 星期四 12:37
    GridView 自定义模版列实现手动编辑功能,删除功能根据这些资料相信你可以很容易写出来了
    实现功能要点:
    1、
    可激发回发事件的控件(比如:Button,LinkButtion,ImageButton等)的 CommandName 属性有几个特殊值:
    Delete 、 Update、Edit、Cancel
    设置成对应的名称 系统自动识别控件激发的事件分别为:
    Delete 激发 RowDeleting 事件
    Update 激发 RowUpdating 事件
    Edit 激发 RowEditing 事件
    Cancel 激发 RowCancelingEdit 事件
    2、
    在GridView 本身的事件中如果对 GridView 进行了
    Delete 、 Update、Edit、Cancel
    等操作 "后" 需要重新绑定数据源


    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    //这里是项模版列中显示内容,如:<%# Eval("title")%>
    </ItemTemplate>
    <EditItemTemplate>
    //编辑状态下的内容,如果这个列中内容编辑模式下需要修改,那么就把修改的内容绑定到能接受用户输入的控件中,如:
    <asp:TextBox ID="txt_title" runat="server" Text='<%#Eval("title")%>'></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>
    </Columns>
    仿照上面的样式设计你的模版列
    下面是编辑列

    <asp:TemplateField>
    <ItemTemplate>
    <asp:LinkButton ID="lbut_edit" CommandName="Edit" runat="server" CausesValidation="false">编辑</asp:LinkButton>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:Button ID="but_edit" runat="server" CommandName="Update" Text="更新" CausesValidation="false" />
    <asp:Button ID="but_cancel" runat="server" CommandName="Cancel" Text="取消" CausesValidation="false" />
    </EditItemTemplate>
    </asp:TemplateField>
    可以看到 编辑列中的控件只不过是普通的linkbuttion 和 buttion 服务器控件
    这样写就能出发编辑事件(GridView_RowEditing)吗?
    是的,原因就在编辑按钮(lbut_edit) 的 CommandName 上,
    控件的 CommandName 参数 有几个 特殊值: Delete 、 Update、Edit、Cancel
    根据单词的意思 也能明白 他们分别代表的特殊的意义了吧
    Delete 激发 RowDeleting 事件
    Update 激发 RowUpdating 事件
    Edit 激发 RowEditing 事件
    Cancel 激发 RowCancelingEdit 事件
    在以上四个事件中 如果有对GridView 的操作
    比如
    在 RowEditing 事件中
    GV_List.EditIndex = e.NewEditIndex;
    设置编辑的行索引 (
    e.NewEditIndex为当前激发RowEditing事件的行索引)
    的操作后 需要重新进行GridView 的绑定:手写的绑定方法 就重新调用一遍,SqlDataSource、
    ObjectDataSource 的话就直接 GV_List.DataBind();就行了
    在取消事件(RowCancelingEdit)里执行
    GV_List.EditIndex = -1;
    当然后面不要忘了重新绑定

    轮到数据更新了(RowUpdating)事件
    在这里获取更新的行里的值、控件的值 调用更新方法
    取值方式
    int row = e.RowIndex;
    Label lab_id = (Label)GV_HelpTypeList.Rows[row].FindControl("lab_id");
    TextBox text = (TextBox)GV_HelpTypeList.Rows[row].FindControl("txt_name");
    用 FindControl 获取到当前行里对应ID 的控件
    调用后台的更新方法吧
    更新完了 再写遍
    RowCancelingEdit 事件里的内容吧!
    更新成功
  • 相关阅读:
    on、where、having的区别和关系
    Java知识点补缺
    Hive部署到IDEA报错 Hive Schema version 2.1.0 does not match metastore's schema version 1.2.0 Metastore is not upgraded or corrupt 解决方案
    Hive知识点总结
    区分同步与异步、阻塞与非阻塞
    Hive查询分区元数据,PARTITIONED BY
    单例模式总结
    Sql语句执行顺序
    收藏大数据相关面试题比较好的链接
    实习技能
  • 原文地址:https://www.cnblogs.com/suizhikuo/p/2219047.html
Copyright © 2011-2022 走看看