前台代码:

<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. Spring将多个配置文件引入一个配置文件中

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  2. ODOO从哪里开始??OpenERP的第一根线头儿

    Windows下ODOO源码启动: python odoo-bin -w odoo -r odoo --addons-path=addons,../mymodules --db-filter=mydb ...

  3. httpd启动脚本

    #!/bin/bash # chkconfig: - . /etc/rc.d/init.d/functions if [ -f /etc/sysconfig/httpd ]; then . /etc/ ...

  4. mysql事务,SET AUTOCOMMIT,START TRANSACTION

    http://yulei568.blog.163.com/blog/static/135886720071012444422/ MyISAM不支持 START TRANSACTION | BEGIN ...

  5. php开发api接口

    做过 API 的人应该了解,其实开发 API 比开发 WEB 更简洁,但可能逻辑更复杂,因为 API 其实就是数据输出,不用呈现页面,所以也就不存在 MVC(API 只有 M 和 C),那么我们来探讨 ...

  6. 【freemaker】之判断是否为空,表达式的使用

    测试代码 @Test public void test05(){ try { freemakerUtil.fprint(root, "05.ftl",fn+"05.htm ...

  7. Redis部分数据结构方法小结

    package com.practice.util; import java.util.HashMap; import java.util.List; import java.util.Map; im ...

  8. 【转】C# 使用消息队列,包括远程访问

    出处:http://www.cnblogs.com/80X86/p/5557801.html 近期做一个小的功能需求,用到了队列,用的时候出了很多问题,现在总结一下,希望能对有需要的人提供帮助. 我的 ...

  9. ThinkPHP 3.2.3心得

    个人还是蛮喜欢tp的比其他的php框架轻(只接触过drupal.tp),而且上手容易(struts这种action的方式,对于java程序员来说).目录结构也比较简单易懂,提供的一些函数也比较实用.对 ...

  10. java和Javascript的区别

    1 首先,这两个家伙没有任何的血缘关系,java是是由Sun 公司于1995年5月推出的,而javascript是于1995年由Netscape公司设计实现而成的,由于Netscape公司与Sun公司 ...