欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net中DetailsView的使用方法

程序员文章站 2024-03-07 11:16:57
aspx页面: 复制代码 代码如下:
aspx页面:
复制代码 代码如下:

<asp:detailsview id="detailsview1" runat="server" autogeneraterows="false" height="50px"
width="500px" onmodechanging="detailsview1_modechanging" onitemdeleting="detailsview1_itemdeleting"
onitemupdating="detailsview1_itemupdating" oniteminserting="detailsview1_iteminserting">
<fields>
<asp:templatefield headertext="id">
<itemtemplate>
<%#eval("id") %>
</itemtemplate>
<insertitemtemplate>
<asp:textbox id="txtinsertid" text="insertid" runat="server"/>
</insertitemtemplate>
<edititemtemplate>
<%#eval("id") %>
</edititemtemplate>
</asp:templatefield>
<asp:templatefield headertext="title">
<itemtemplate>
<%#eval("title") %>
</itemtemplate>
<insertitemtemplate>
<asp:textbox id="txtinserttitle" text="inserttitle" runat="server"/>
</insertitemtemplate>
<edititemtemplate>
<asp:textbox id="txtedittitle" text='<%# eval("title") %>' runat="server"/>
</edititemtemplate>
</asp:templatefield>
<asp:templatefield headertext="context">
<itemtemplate>
<%# eval("logcontext") %>
</itemtemplate>
<insertitemtemplate>
<asp:textbox id="txtinsertlogcontext" text="insertlogcontext" runat="server"/>
</insertitemtemplate>
<edititemtemplate>
<asp:textbox id="txteditlogcontext" text='<%# eval("logcontext") %>' runat="server"/>
</edititemtemplate>
</asp:templatefield>
<asp:templatefield headertext="操作">
<itemtemplate>
<asp:button id="btnedit" runat="server" causesvalidation="false"
commandname="edit" text="编辑" />
<asp:button id="btnnew" runat="server" causesvalidation="false"
commandname="new" text="新建" />
<asp:button id="btndelete" runat="server" causesvalidation="false"
commandname="delete" text="删除" onclientclick="return confirm('确定要更新该学生信息吗?');" />
</itemtemplate>
<insertitemtemplate>
<asp:button id="btninsert" runat="server" causesvalidation="true"
commandname="insert" text="插入" />
<asp:button id="btncancel" runat="server" causesvalidation="false"
commandname="cancel" text="取消" />
</insertitemtemplate>
<edititemtemplate>
<asp:button id="btnupdate" runat="server" causesvalidation="true"
commandname="update" text="更新" onclientclick="return confirm('确定要更新该学生信息吗?');" />
<asp:button id="btncancel2" runat="server" causesvalidation="false"
commandname="cancel" text="取消" />
</edititemtemplate>
</asp:templatefield>
</fields>
</asp:detailsview>


cs文件:
复制代码 代码如下:

using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.collections.generic;
public partial class _default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
if (!ispostback)
{
datatobing();
}
}
private void datatobing()
{
this.detailsview1.datasource = datagridsource();
this.detailsview1.databind();
}
private list<log> datagridsource()
{
list<log> logs = new list<log>();
for (int i = 1; i < 11; i++)
{
log log = new log();
log.id = i;
log.title = "标题" + i;
log.logcontext = "内容" + i;
logs.add(log);
}
return logs;
}
public class log
{
private int id;
public int id
{
get { return id; }
set { id = value; }
}
private string title;
public string title
{
get { return title; }
set { title = value; }
}
private string logcontext;
public string logcontext
{
get { return logcontext; }
set { logcontext = value; }
}
}
protected void detailsview1_modechanging(object sender, detailsviewmodeeventargs e)
{
this.detailsview1.changemode(e.newmode);
datatobing();
}
protected void detailsview1_itemdeleting(object sender, detailsviewdeleteeventargs e)
{
response.write("删除操作");
}
protected void detailsview1_itemupdating(object sender, detailsviewupdateeventargs e)
{
textbox tbtitle = (textbox)this.detailsview1.findcontrol("txtedittitle");
textbox tblogcontext = (textbox)this.detailsview1.findcontrol("txtedittitle");
response.write("更新操作 : title : " + tbtitle.text + " : logcontext : " + tblogcontext.text);
}
protected void detailsview1_iteminserting(object sender, detailsviewinserteventargs e)
{
response.write("插入操作 : ");
}
}