时间:2021-05-28
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("插入操作 : ");
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了ASP.NET动态增加HTML元素的方法。分享给大家供大家参考,具体如下:在使用asp.net进行web开发的时候页面中的中的信息可以通过asp.
本文实例讲述了ASP.NET使用TreeView显示文件的方法,是非常实用的技巧。分享给大家供大家参考。具体实现方法如下:通常在ASP.NET中,TreeVie
本文实例讲述了ASP.NET抓取网页内容的实现方法。分享给大家供大家参考。具体实现方法如下:一、ASP.NET使用HttpWebRequest抓取网页内容复制代
继续上篇文章的学习《灵活掌握asp.net中gridview控件的多种使用方法(上)》,在此基础上巩固gridview控件的操作使用,更上一层楼。11.Grid
我们开发ASP.NET站点时,如果将jQueryUIDatepicker与ASP.NET的验证控件(如:RequiredFieldValidator)组合使用: