首页 > 编程 > .NET > 正文

asp.net中DetailsView的使用方法

2024-07-10 13:25:33
字体:
来源:转载
供稿:网友
aspx页面:

复制代码 代码如下:


<asp:DetailsView runat="server" AutoGenerateRows="False"
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 Text="InsertId" runat="server"/>
</InsertItemTemplate>
<EditItemTemplate>
<%#Eval("Id") %>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<%#Eval("Title") %>
</ItemTemplate>
<InsertItemTemplate>
<asp:TextBox Text="InsertTitle" runat="server"/>
</InsertItemTemplate>
<EditItemTemplate>
<asp:TextBox Text='<%# Eval("Title") %>' runat="server"/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Context">
<ItemTemplate>
<%# Eval("LogContext") %>
</ItemTemplate>
<InsertItemTemplate>
<asp:TextBox Text="InsertLogContext" runat="server"/>
</InsertItemTemplate>
<EditItemTemplate>
<asp:TextBox Text='<%# Eval("LogContext") %>' runat="server"/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:Button runat="server" CausesValidation="False"
CommandName="Edit" Text="编辑" />
<asp:Button runat="server" CausesValidation="False"
CommandName="New" Text="新建" />
<asp:Button runat="server" CausesValidation="False"
CommandName="Delete" Text="删除" OnClientClick="return confirm('确定要更新该学生信息吗?');" />
</ItemTemplate>
<InsertItemTemplate>
<asp:Button runat="server" CausesValidation="True"
CommandName="Insert" Text="插入" />
<asp:Button runat="server" CausesValidation="False"
CommandName="Cancel" Text="取消" />
</InsertItemTemplate>
<EditItemTemplate>
<asp:Button runat="server" CausesValidation="True"
CommandName="Update" Text="更新" OnClientClick="return confirm('确定要更新该学生信息吗?');" />
<asp:Button 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 { + tbTitle.Text + " : LogContext : " + tbLogContext.Text);
}
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
Response.Write("插入操作 : ");
}
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表