前台代码:

<div>
<asp:DetailsView ID="gvDepart" runat="server" AutoGenerateRows="False" OnItemDeleting="gvDepart_ItemDeleting" OnItemInserting="gvDepart_ItemInserting" OnItemUpdating="gvDepart_ItemUpdating" OnModeChanging="gvDepart_ModeChanging" DataKeyNames="DepartId">
<Fields>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("DepartId") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("DepartId") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("DepartId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("DepartName") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("DepartName") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("DepartName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("ParentId") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("ParentId") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("ParentId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="操作" ShowHeader="False">
<InsertItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Insert" Text="插入"></asp:LinkButton>
 <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消"></asp:LinkButton>
</InsertItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="New" Text="新建"></asp:LinkButton>
<asp:LinkButton ID="btnDelete" CausesValidation="false" CommandName="Delete" CommandArgument='<%# Eval("DepartId") %>' Text="删除" runat="server" />
<asp:LinkButton ID="btnUpdate" CausesValidation="false" CommandName="Edit" CommandArgument='<%# Eval("DepartId") %>' Text="修改" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" CommandArgument='<%# Eval("DepartId") %>' Text="修改"></asp:LinkButton>
 <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
</div>

后台代码:

public partial class WebForm4 : System.Web.UI.Page
{
DepartmentInfoBLL bll = new DepartmentInfoBLL();
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["departId"] == null) {
Response.Redirect("WebForm3.aspx");
}
if (!IsPostBack) {
InitDepart();
}
} private void InitDepart()
{
int departId = int.Parse(Request.QueryString["departId"]);
IList<DepartmentInfo> list = bll.GetByDepartId(departId);
this.gvDepart.DataSource = list;
this.gvDepart.DataBind();
} protected void gvDepart_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
int departId = int.Parse(e.CommandArgument.ToString());
string departName = (this.gvDepart.Rows[1].FindControl("TextBox2") as TextBox).Text;
int parentId =int.Parse((this.gvDepart.Rows[2].FindControl("TextBox3") as TextBox).Text);
//执行修改的方法
bll.Update(new DepartmentInfo() { DepartId=departId,DepartName=departName,ParentId=parentId,Remark=""});
//返回到只读模式
this.gvDepart.ChangeMode(DetailsViewMode.ReadOnly);
//重新绑定数据
InitDepart();
} protected void gvDepart_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
int departId = int.Parse((this.gvDepart.Rows[0].FindControl("TextBox1") as TextBox).Text);
string departName = (this.gvDepart.Rows[1].FindControl("TextBox2") as TextBox).Text;
int parentId = int.Parse((this.gvDepart.Rows[2].FindControl("TextBox3") as TextBox).Text);
//执行添加的方法
bll.Insert(new DepartmentInfo() { DepartId=departId,DepartName=departName,ParentId=parentId,Remark=""});
//跳转到转到详情页面的前面一个页面
Response.Redirect("~/WebForm3.aspx");
} protected void gvDepart_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
{
int departId = int.Parse(e.Keys[0].ToString());
//执行删除的方法
bll.DeleteByDepartId(departId);
//跳转到转到详情页面的前面一个页面
Response.Redirect("~/WebForm3.aspx");
} protected void gvDepart_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
//改变模式
this.gvDepart.ChangeMode(e.NewMode);
InitDepart();
}
}

  

DetailsView的添加,修改,删除,查询的更多相关文章

  1. Xamarin.Android 入门实例(4)之实现对 SQLLite 进行添加/修改/删除/查询操作

    1.Main.axml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: ...

  2. JavaWeb_day03_员工信息添加修改删除

    day03员工的添加,修改,删除 修改功能 思路 : 点击修改员工数据之后,跳转到单行文本,查询要修改的员工id的全部信息,主键id设置为readonly,其余的都可以修改, 修改之后,提交按钮,提交 ...

  3. Web 1三级联动 下拉框 2添加修改删除 弹框

    Web  三级联动 下拉框 using System; using System.Collections.Generic; using System.Linq; using System.Web; u ...

  4. 打通前后端全栈开发node+vue进阶【课程学习系统项目实战详细讲解】(3):用户添加/修改/删除 vue表格组件 vue分页组件

    第三章 建议学习时间8小时      总项目预计10章 学习方式:详细阅读,并手动实现相关代码(如果没有node和vue基础,请学习前面的vue和node基础博客[共10章] 演示地址:后台:demo ...

  5. SpringBoot定时任务升级篇(动态添加修改删除定时任务)

    需求缘起:在发布了<Spring Boot定时任务升级篇>之后得到不少反馈,其中有一个反馈就是如何动态添加修改删除定时任务?那么我们一起看看具体怎么实现,先看下本节大纲: (1)思路说明: ...

  6. 【转】C#添加修改删除文件文件夹大全

    [转]C#添加修改删除文件文件夹大全 C#添加修改删除文件文件夹大全 StreamWriter sw = File.AppendText(Server.MapPath(".")+& ...

  7. Oracle的学习二:表管理(数据类型、创建/修改表、添加/修改/删除数据、数据查询)

    1.Oracle表的管理 表名和列名的命名规则: 必须以字母开头: 长度不能超过30个字符: 不能使用oracle的保留字: 只能使用如下字符:A-Z, a-z, 0-9, $, # 等. Oracl ...

  8. SQL Server 数据的添加修改删除和查询

    数据的添加: 首先建立一个数据库,点击新建查询,然后用代码建立一个表,表里写上列名和数据类型,约束可加可不加 然后使用insert语句往表里添加数据 insert [into] 表名 (列名1,列名2 ...

  9. oracle JOB 查询 添加 修改 删除

    -------------查询JOB----------------- select job, what, next_date, next_sec, sysdate, failures, broken ...

  10. Android 查询 添加 修改 删除通讯录联系人示例

    1.AndroidManifest.xml中添加权限 <uses-permission android:name="android.permission.READ_CONTACTS&q ...

随机推荐

  1. JQUERY添加、删除元素、eq()方法;

    一.jQuery - 添加元素 1.append() - 在被选元素内部的结尾插入指定内容 2.prepend() - 在被选元素内部的开头插入指定内容 3.after() - 在被选元素之后插入内容 ...

  2. JSBinding / About JSComponent and Serialization

    About JSComponent JSCompnent is a normal Unity script. It inherits from JSSerializer and JSSerialize ...

  3. Maven学习8-使用Maven构建多模块项目

    在平时的Javaweb项目开发中为了便于后期的维护,我们一般会进行分层开发,最常见的就是分为domain(域模型层).dao(数据库访问 层).service(业务逻辑层).web(表现层),这样分层 ...

  4. SQL总结(六)触发器

    SQL总结(六)触发器 概念 触发器是一种特殊类型的存储过程,不由用户直接调用.创建触发器时会对其进行定义,以便在对特定表或列作特定类型的数据修改时执行. 触发器可以查询其他表,而且可以包含复杂的 S ...

  5. < 独立项目 - 文本挖掘 > - 2016/10/25 第一更 - <Linux相关知识准备>

    < 独立项目 -  文本挖掘 > 项目立项的相关背景介绍,TODO方向. 一.Ubuntu环境配置 主机系统:Windows 7 SP1  64位操作系统 | i5-4210 CPU | ...

  6. Difference between applicationContext.xml and spring-servlet.xml in Spring Framework

    Question: Are applicationContext.xml and spring-servlet.xml related anyhow in Spring Framework? Will ...

  7. php_sapi_name详解

    php_sapi_name : — 返回 web 服务器和 PHP 之间的接口类型. 返回描述 PHP 所使用的接口类型(the Server API, SAPI)的小写字符串. 例如,CLI 的 P ...

  8. PLSQL转义字符

    http://blog.csdn.net/cunxiyuan108/article/details/5800800

  9. 一些代码 II (ConfigParser、创建大文件的技巧、__getattr__和__getattribute__、docstring和装饰器、抽象方法)

    1. ConfigParser format.conf [DEFAULT] conn_str = %(dbn)s://%(user)s:%(pw)s@%(host)s:%(port)s/%(db)s ...

  10. 数据库的索引和填充因子fillfactor

    索引分为聚簇索引和非聚簇索引 1.聚簇索引/聚集索引 聚簇索引的顺序就是数据的物理存储顺序,对于一个表来说,只有一个聚簇索引 create unique clustered index id_inde ...