如何:允许用户编辑 DataList Web 服务器控件中的项
更新:2007 年 11 月
可以允许用户编辑 DataList Web 服务器控件中的单个项。将单个项设置为编辑模式时,可以更改的值通常显示在文本框或其他用户可在其中进行更改的控件中。
允许用户编辑 DataList 控件中的项
将 DataList 控件的 DataKeyField 属性设置为包含主键的数据中的字段名称。
创建 ItemTemplate(如果您正在使用它,则需再创建 AlternatingItemTemplate),然后将 Button Web 服务器控件添加到其中。将此按钮的 CommandName 属性设置为 edit。
说明: 可以在调用 Button Web 服务器控件的任何步骤中使用 LinkButton 或 ImageButton 控件。
为包括以下各项的 DataList 控件创建 EditItemTemplate:
用户可更改的所有值的控件。例如,包括所有字符和数值数据的 TextBox 控件。使用声明性 Eval 方法指定每个控件所绑定到的字段,如该示例所示:
安全说明: 该示例具有一个文本框,用于接受用户输入,这是一个潜在的安全威胁。默认情况下,ASP.NET 网页验证用户输入是否不包括脚本或 HTML 元素。有关更多信息,请参见脚本侵入概述。
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("ProductName") %>' />
Button 控件,其 Text 属性设置为“Update”,其 CommandName 属性设置为 update(区分大小写)。
Button 控件,其 Text 属性设置为“Cancel”,其 CommandName 属性设置为 cancel。
**“更新”按钮允许用户指定他们已完成编辑并将保存任何更改。“取消”**按钮将允许用户在不保存更改的情况下退出编辑。
编写执行下列功能的代码:
处理 DataList 控件的 EditCommand 事件,该事件将 DataList 控件的 EditItemIndex 属性设置为要置于编辑模式的项的索引值。用户所单击项的索引可以通过 Item 对象的 ItemIndex 属性获得。然后调用该控件的 DataBind 方法。
处理 DataList 控件的 CancelCommand 事件,该事件将 DataList 控件的 EditItemIndex 属性设置为 -1,然后再调用该控件的 DataBind 方法。
处理 DataList 控件的 UpdateCommand 事件。在此代码中,从当前项的控件中提取值并将其传递至数据源控件中,以用于更新操作。您所应用的确切代码将取决于您所使用的数据源控件的类型。
示例
下面的代码示例演示一个 ASP.NET 页面,该页面使用 DataList 控件和 SqlDataSource 控件显示 Northwind 数据库中的 Categories 表信息。用户可以编辑这些项。
安全说明: |
---|
该示例具有一个文本框,用于接受用户输入,这是一个潜在的安全威胁。默认情况下,ASP.NET 网页验证用户输入是否不包括脚本或 HTML 元素。有关更多信息,请参见脚本侵入概述。 |
<%@ Page Language="VB" %>
<script runat="server">
Protected Sub DataList1_EditCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)
DataList1.EditItemIndex = e.Item.ItemIndex
DataList1.DataBind()
End Sub
Protected Sub DataList1_CancelCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)
DataList1.EditItemIndex = -1
DataList1.DataBind()
End Sub
Protected Sub DataList1_UpdateCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)
Dim categoryID As String = _
DataList1.DataKeys(e.Item.ItemIndex).ToString()
Dim categoryName As TextBox = _
CType(e.Item.FindControl("textCategoryName"), TextBox)
Dim description As TextBox = _
CType(e.Item.FindControl("textDescription"), TextBox)
SqlDataSource1.UpdateParameters("original_CategoryID"). _
DefaultValue = categoryID
SqlDataSource1.UpdateParameters("categoryName"). _
DefaultValue = categoryName.Text
SqlDataSource1.UpdateParameters("Description"). _
DefaultValue = description.Text
SqlDataSource1.Update()
DataList1.EditItemIndex = -1
DataList1.DataBind()
End Sub
</script>
<html>
<head runat="server"></head>
<body>
<form id="form1" runat="server">
<div>
<br />
<asp:DataList runat="server"
DataKeyField="CategoryID"
DataSourceID="SqlDataSource1" ID="DataList1"
OnEditCommand="DataList1_EditCommand"
OnCancelCommand="DataList1_CancelCommand"
OnUpdateCommand="DataList1_UpdateCommand">
<EditItemTemplate>
ID: <asp:Label ID="Label1" runat="server"
Text='<%# Eval("CategoryID") %>'>
</asp:Label>
<br />
Name: <asp:TextBox ID="textCategoryName" runat="server"
Text='<%# Eval("CategoryName") %>'>
</asp:TextBox>
<br />
Description: <asp:TextBox ID="textDescription"
runat="server"
Text='<%# Eval("Description") %>'>
</asp:TextBox>
<br />
<asp:LinkButton ID="LinkButton1" runat="server"
CommandName="update" >
Save
</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server">
CommandName="cancel"
Cancel
</asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
CategoryID:
<asp:Label ID="CategoryIDLabel" runat="server"
Text='<%# Eval("CategoryID") %>'>
</asp:Label>
<br />
CategoryName:
<asp:Label ID="CategoryNameLabel" runat="server"
Text='<%# Eval("CategoryName") %>'>
</asp:Label>
<br />
Description:
<asp:Label ID="DescriptionLabel" runat="server"
Text='<%# Eval("Description") %>'>
</asp:Label>
<br />
<asp:LinkButton runat="server" ID="LinkButton1"
CommandName="edit" >
Edit
</asp:LinkButton><br />
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString=
"<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [CategoryID], [CategoryName],
[Description] FROM [Categories]"
UpdateCommand="UPDATE [Categories] SET [CategoryName] =
@CategoryName, [Description] = @Description
WHERE [CategoryID] = @original_CategoryID">
<UpdateParameters>
<asp:Parameter Name="CategoryName" Type="String" />
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="original_CategoryID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<script runat="server">
protected void DataList1_EditCommand(object source,
DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
DataList1.DataBind();
}
protected void DataList1_CancelCommand(object source,
DataListCommandEventArgs e)
{
DataList1.EditItemIndex = -1;
DataList1.DataBind();
}
protected void DataList1_UpdateCommand(object source,
DataListCommandEventArgs e)
{
String categoryID =
DataList1.DataKeys[e.Item.ItemIndex].ToString();
String categoryName =
((TextBox)e.Item.FindControl("textCategoryName")).Text;
String description =
((TextBox) e.Item.FindControl("textDescription")).Text;
SqlDataSource1.UpdateParameters["original_CategoryID"].DefaultValue
= categoryID;
SqlDataSource1.UpdateParameters["categoryName"].DefaultValue
= categoryName;
SqlDataSource1.UpdateParameters["Description"].DefaultValue
= description;
SqlDataSource1.Update();
DataList1.EditItemIndex = -1;
DataList1.DataBind();
}
</script>
<html>
<head runat="server"></head>
<body>
<form id="form1" runat="server">
<div>
<br />
<asp:DataList runat="server"
DataKeyField="CategoryID"
DataSourceID="SqlDataSource1" ID="DataList1"
OnEditCommand="DataList1_EditCommand"
OnCancelCommand="DataList1_CancelCommand"
OnUpdateCommand="DataList1_UpdateCommand">
<EditItemTemplate>
ID: <asp:Label ID="Label1" runat="server"
Text='<%# Eval("CategoryID") %>'>
</asp:Label>
<br />
Name: <asp:TextBox ID="textCategoryName" runat="server"
Text='<%# Eval("CategoryName") %>'>
</asp:TextBox>
<br />
Description: <asp:TextBox ID="textDescription"
runat="server"
Text='<%# Eval("Description") %>'>
</asp:TextBox>
<br />
<asp:LinkButton ID="LinkButton1" runat="server"
CommandName="update" >
Save
</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server">
CommandName="cancel"
Cancel
</asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
CategoryID:
<asp:Label ID="CategoryIDLabel" runat="server"
Text='<%# Eval("CategoryID") %>'>
</asp:Label>
<br />
CategoryName:
<asp:Label ID="CategoryNameLabel" runat="server"
Text='<%# Eval("CategoryName") %>'>
</asp:Label>
<br />
Description:
<asp:Label ID="DescriptionLabel" runat="server"
Text='<%# Eval("Description") %>'>
</asp:Label>
<br />
<asp:LinkButton runat="server" ID="LinkButton1"
CommandName="edit" >
Edit
</asp:LinkButton><br />
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString=
"<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [CategoryID], [CategoryName],
[Description] FROM [Categories]"
UpdateCommand="UPDATE [Categories] SET [CategoryName] =
@CategoryName, [Description] = @Description
WHERE [CategoryID] = @original_CategoryID">
<UpdateParameters>
<asp:Parameter Name="CategoryName" Type="String" />
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="original_CategoryID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
若要更新数据,您需要具有要更新的记录的主键。您可以从包含键数组的 DataKeyField 属性中获取该值。
若要获取该项中特定控件的值,请使用 Item 事件参数对象的 FindControl 方法。
在 SqlDataSource1.UpdateParameters 字典中设置的值必须与在 UpdateParameters 元素中设置的名称匹配。
编译代码
代码要求您具有名为 NorthwindConnectionString 的连接字符串。您所连接的数据库假定具有名为 Categories 的表,该表具有字段 CategoryID、CategoryName 和 Description。
页面连接数据库所使用的帐户必须具有更新 Category 表的权限。
可靠编程
该示例中的代码将不执行您通常在生产环境中执行的下列任务:
代码中不包括用以确保 FindControl 方法返回了有效控件的错误检查。要获得功能更强大的代码,请确保 FindControl 方法返回的值不是空引用(在 Visual Basic 中为 Nothing)。
代码将不检查更新是否成功。
安全性
Web 窗体页中的用户输入可能包括潜在有害的客户端脚本。默认情况下,Web 窗体页验证用户输入是否不包括脚本或 HTML 元素。有关更多信息,请参见脚本侵入概述。