前台:
<%@ Page language="c#" Inherits="CommonFunction.DataListEditItem" CodeFile="DataListEditItem.aspx.cs" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DataListEditItem</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<h2>使用DataList控件的编辑模板</h2>
<asp:DataList id="dlEditItem" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 48px"
runat="server" Height="136px" DataKeyField="EmployeeID">
<HeaderTemplate>人员信息</HeaderTemplate> /*头模板*/
<FooterTemplate><hr color="red"></FooterTemplate> /*底部模板*/
<ItemTemplate>
<asp:Button id="edit" CommandName="edit" Text="编辑" Runat="server"></asp:Button>
<%#DataBinder.Eval(Container.DataItem,"LastName")%><%#DataBinder.Eval(Container.DataItem,"FirstName")%>
</ItemTemplate>
<EditItemTemplate>
<asp:Label id="lastname" Runat="server">
<%#DataBinder.Eval(Container.DataItem,"LastName")%>
</asp:Label>
<asp:Label id="FirstName" Runat="server">
<%#DataBinder.Eval(Container.DataItem,"FirstName")%>
</asp:Label>
<asp:TextBox id=Title runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Title")%>'>
</asp:TextBox>
<asp:TextBox id=titleOfCourtesy runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"TitleOfCourtesy")%>'>
</asp:TextBox>
<asp:Button id="update" CommandName="update" Text="更新" Runat="server"></asp:Button>
<asp:Button id="cancel" CommandName="cancel" Text="取消" Runat="server"></asp:Button>
</EditItemTemplate>
</asp:DataList>
</form>
</body>
</HTML>
效果图:


后台:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace CommonFunction
{
///<summary>
/// DataListEditItem 的摘要说明。
///</summary>
public partial class DataListEditItem : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
//页面初试化时进行数据绑定
if(!IsPostBack)
DataListDataBind();
}
private void DataListDataBind()
{
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
//创建数据适配器对象
SqlDataAdapter da = new SqlDataAdapter("select EmployeeID,LastName,FirstName,Title,TitleOfCourtesy from Employees",conn);
//创建DataSet对象
DataSet ds = new DataSet();
try
{
//填充数据集
da.Fill(ds,"testTable");
//进行数据绑定
dlEditItem.DataSource = ds.Tables["testTable"];
dlEditItem.DataBind();
}
catch(Exception error)
{
//输出异常信息
Response.Write(error.ToString());
}
}
private void dlEditItem_CancelCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
//设置DataList控件的编辑项的索引为-1,既取消编辑
dlEditItem.EditItemIndex = -1;
//数据绑定
DataListDataBind();
}
private void dlEditItem_EditCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
//设置DataList控件的编辑项的索引为选择的当前项
dlEditItem.EditItemIndex = e.Item.ItemIndex;
//数据绑定
DataListDataBind();
}
private void dlEditItem_UpdateCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
//取得编辑行的关键字段的值
int empID = (int)dlEditItem.DataKeys[e.Item.ItemIndex];
//取得文本框中输入的内容
TextBox newTitle = (TextBox)e.Item.FindControl("Title");
TextBox newTitleOfCour = (TextBox)e.Item.FindControl("TitleOfCourtesy");
//定义SQL语句
string sqlCom = "update Employees set Title='"+newTitle.Text+"',TitleOfCourtesy='"+newTitleOfCour.Text+"' where EmployeeID="+empID.ToString();
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
//定义命令对象
SqlCommand cmd = new SqlCommand(sqlCom,conn);
//打开数据连接
conn.Open();
try
{
//执行SQL命令
cmd.ExecuteNonQuery();
//取消编辑
dlEditItem.EditItemIndex = -1;
DataListDataBind();
}
catch(Exception err)
{
//输入异常信息
Response.Write(err.ToString());
}
finally
{
//关闭连接对象
conn.Close();
}
}
}
}
本文介绍如何使用ASP.NET中的DataList控件实现数据编辑功能。通过示例代码展示了DataList控件的基本配置、编辑模板的使用及后台代码实现数据的加载、编辑、更新和取消操作。
251

被折叠的 条评论
为什么被折叠?



