asp.net中使用DetailsView的教程

2019-09-08 09:44:40来源:爱站网 阅读 ()

新老客户大回馈,云服务器低至5折

对于asp.net中使用DetailsView的教程是不是有很多小伙伴们都不了解呢?接下来本文小编就将为大家介绍asp.net中如何使用DetailsView的解决方法,感兴趣的小伙伴们一起来参考参考。

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("插入操作 : ");
}
}

asp.net中使用DetailsView的教程就到这里了,?更多新知识与亮点等着大家去挖掘,今后爱站小编也将为大家分享更多asp.net技巧。


原文链接:https://js.aizhan.com/develop/aspnet/8749.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:ASP.NET中生成Excel遇到的问题及改进方法

下一篇:详解asp.net下加密Config的方法