前台代码:

<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. 切换到android studio环境

    下载: android-studio-bundle-135.1740770-windows.exe sysimg_arm-21_r03.zip 设置环境变量: ANDROID_SDK_HOME     ...

  2. java-String基础篇

    一.String字符串理解 java字符串类,包含了字符串的值和实现字符串相关操作的一些方法 1.String字符串可分静态字符串和动态字符串 静态初始化字符串:String s1 = "h ...

  3. 5、StringBuffer与StringBuilder的区别(十进制转二进制)

    1. 在执行速度方面的比较:StringBuilder > StringBuffer 2. StringBuffer与StringBuilder,他们是字符串变量,是可改变的对象,每当我们用它们 ...

  4. 国内首家VR虚拟现实主题公园即将在北京推出

    近期,美国“The VOID”.澳洲“Zero Latency”两大虚拟现实主题乐园让许多爱好者兴奋至极,门票据说都已经预约到明年2月!在如此巨大的商机面前,谁将抢到国内VR虚拟现实主题公园第一块蛋糕 ...

  5. WPF外包公司——北京动点飞扬软件:开发企业WPF项目需要掌握些什么

    做为企业开发一个WPF项目,对于很多不熟悉微软WPF技术和XAML语言开发团队而言,北京动点飞扬在此给各位一点建议: 1.首先开发团队要整体对于XAML和WPF的运作机制熟悉. 2.开发人员起码要会用 ...

  6. JavaScript常用函数和方法

    alert('Hello World!') //方法用于显示带有一条指定消息和一个 OK 按钮的警告框. //定义js函数 function Foo(name) { console.log(name) ...

  7. java中向JTextArea中添加滚动条(垂直的和水平的)

    这次在研究java的swing类时,在做一个实例时,发现了JTextArea中添加滚动条的问题,经过网上查找资料和自己测试发现有以下用法: 1.首先应该把JTextArea添加到一个JScrollPa ...

  8. linux 下的clock_gettime() 获取精确时间函数

    #include <time.h> int clock_gettime(clockid_t clk_id, struct timespec* tp); clock_gettime() 函数 ...

  9. jquery是如何架构的.

    心里一直有个疑问. jquery是如何做到一个jQuery即可以当方法用比如$();又可以当对象用$.extend(); 现在总结一下吧 function method(){} var m=new m ...

  10. MFC webbrowser读取文档的meta分析

    IDispatch* pDisp = NULL; IDispatch* pDisp2 = NULL; IHTMLDocument2 *pHtmlDoc2 = NULL; IHTMLElementCol ...