可以允许用户编辑 DataList Web 服务器控件中的单个项。当将单个项置于编辑模式中时,其可编辑值通常显示在用户可更改值的文本框或其他控件中。允许用户编辑 DataList 控件中的项 创建 ItemTemplate(和 AlternatingItemTemplate,如果要使用它的话),然后向其添加 Button Web 服务器控件。将此按钮的 CommandName 属性设置为 edit(区分大小写)。有关详细信息,请参见创建 Web 服务器控件模板。 注意 可以在调用 Button 的任何步骤中使用 LinkButton 或 ImageButton。 为 DataList 控件创建 EditItemTemplate,它包括下列内容: 用户可更改的所有值的控件。例如,它包括所有字符和数值数据的 TextBox 控件。使用 DataBinding 属性设置这些控件的数据源。有关信息,请参见 Web 窗体页中的数据访问。 Text 属性为“更新”的 Button 控件,该控件的 CommandName 属性设置为 update(区分大小写)。 Text 属性为“取消”的 Button 控件,该控件的 CommandName 属性设置为 cancel(区分大小写)。 “更新”按钮将使用户得以指定他们已完成编辑。“取消”按钮将允许用户在不保存更改的情况下退出编辑。 为控件的 EditCommand 事件创建一个事件处理程序。有关详细信息,请参见在 Web 窗体页中创建事件处理程序。在事件处理程序中,执行下列操作: 将 DataList 控件的 EditItemIndex 属性设置为要转入编辑模式的项的索引值。用户所单击项的索引可以通过 Item 对象的 ItemIndex 属性获得。 将控件绑定到它的数据源。 该事件处理程序类似如下所示: ' Visual Basic Private Sub DataList1_EditCommand(ByVal source As Object, _ ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) _ Handles DataList1.EditCommand DataList1.EditItemIndex = e.Item.ItemIndex DataList1.DataBind() End Sub// C# private void DataList1_EditCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e) { DataList1.EditItemIndex = e.Item.ItemIndex; DataList1.DataBind(); } 为 UpdateCommand 事件创建一个方法,该方法读取当前项中的编辑控件值,并将其写回到数据源。若要获取该项中特定控件的值,请使用 Item 事件参数对象的 Control.FindControl 方法。更新数据源后,通过将 EditItemIndex 属性设置为 -1,将该项切换出编辑模式,然后重新绑定到数据源。 安全说明 Web 窗体页中的用户输入可能包含潜在的恶意客户端脚本。默认情况下,Web 窗体页验证用户输入是否不包括脚本或 HTML 元素。有关更多信息,请参见脚本利用和在 Web 应用程序中防止脚本利用。 ' Visual Basic Private Sub DataList1_UpdateCommand(ByVal source As Object, _ ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) _ Handles DataList1.UpdateCommand Dim quantity As TextBox quantity = CType(e.Item.FindControl("quantity"), TextBox) Dim newQuantity As String = quantity.Text ' Add code here to update the Quantity value of the data source ' using the newQuantity variable. Note that newQuantity is a ' string and would have to be converted to a number. DataList1.EditItemIndex = -1 DataList1.DataBind() End Sub// C# private void DataList1_UpdateCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e) { TextBox quantity; quantity = (TextBox)(e.Item.FindControl("Quantity")); string newQuantity = quantity.Text; // Add code here to update the Quantity value of the data source // using the newQuantity variable. Note taht newQuantity is a // string and would have to be converted to a number. DataList1.EditItemIndex = -1; DataList1.DataBind(); } 为 CancelCommand 事件创建一个方法,该方法将 EditItemIndex 属性设置为 -1,然后重新绑定到数据源: ' Visual Basic Private Sub DataList1_CancelCommand(ByVal source As Object, _ ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) _ Handles DataList1.CancelCommand ' Switch out of edit mode. DataList1.EditItemIndex = -1 DataList1.DataBind() End Sub// C# private void DataList1_CancelCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e) { // Switch out of edit mode. DataList1.EditItemIndex = -1; DataList1.DataBind(); }
可允许用户以各种方法删除 DataList 控件中的项。一种方法是在项中包含“删除”按钮,当用户单击它时立即删除该项。另一种方法是在单个项中包括复选框。用户随后可以选中要移除的所有项,然后单击单独的“删除”按钮成批删除它们。此方法在如 MSN Hotmail 这样的程序中使用。允许用户删除单个项 在 ItemTemplate 中(和 AlternatingItemTemplate 中,如果要使用它的话),添加一个 Button 或 LinkButton Web 服务器控件。将此按钮的 CommandName 属性设置为 delete(区分大小写)。 为 DeleteCommand 事件创建事件处理程序。有关详细信息,请参见在 Web 窗体页中创建事件处理程序。在此方法中: 从数据源中删除项,然后重新绑定 DataList 控件。用户所单击项的索引可以通过 Item 对象的 ItemIndex 属性获得。若要获取单个控件的值,请使用 Item 事件参数对象的 FindControl 方法。 将控件重新绑定到它的数据源。 如果数据源是 DataTable 对象,则事件处理程序可能类似如下所示: ' Visual Basic Private Sub DataList1_DeleteCommand(ByVal source As Object, _ ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) _ Handles DataList1.DeleteCommand ' Code to delete the item from the data source. Dim aTable As DataTable aTable = CType(DataList1.DataSource, DataTable) aTable.Rows(e.Item.ItemIndex).Delete() ' Bind the data after item is deleted. DataList1.DataBind() End Sub// C# private void DataList1_DeleteCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e) { // Code to delete the item from the data source. DataTable aTable = (DataTable)DataList1.DataSource; aTable.Rows[e.Item.ItemIndex].Delete(); // Bind the data after the item is deleted. DataList1.DataBind(); } 允许用户一次删除多个项 在 ItemTemplate(如果使用,还有 AlternatingItemTemplate)中,添加 CheckBox Web 服务器控件并将其 ID 属性设置为“删除”。请确保该 CheckBox 控件的 AutoPostBack 属性为 false。通过右击 DataList 控件并从菜单中选择“结束模板编辑”,关闭模板编辑。 将 Button Web 服务器控件添加到 Web 窗体页。将 Text 属性设置为“全部删除”,并将 ID 属性设置为 DeleteAll。此按钮不添加到 DataList 模板之一。 为“全部删除”按钮的 Click 事件创建方法。在方法中: 依次通过 DataList 控件的 Items 集合,按顺序提取每一项。 在项内,使用该项的 FindControl 方法获取步骤 1 的 CheckBox 控件,然后测试其 Checked 属性。 如果该框处于选中状态,则从数据源中删除相应的项。 将 DataList 控件重新绑定到它的数据源。 下面的示例显示 DeleteAll 按钮的事件处理程序,它使用上面所述的过程成批删除项。 ' Visual Basic Private Sub DeleteAll_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles DeleteAll.Click Dim isDeleted As Boolean Dim anItem As DataListItem ' Check each box and see if the item should be deleted. For Each anItem In DataList1.Items isDeleted = CType(anItem.FindControl("Delete"), CheckBox).Checked If isDeleted Then ' Add code here to delete the item, using anItem.ItemIndex. End If Next DataList1.DataBind() End Sub// C# protected void DeleteAll_Click(object sender, System.EventArgs e) { bool isDeleted; // Check each box and see if the item should be deleted. foreach (DataListItem anItem in DataList1.Items) { isDeleted = ((CheckBox)anItem.FindControl("Delete")).Checked; if (isDeleted) { // Add code here to delete the item, using anItem.ItemIndex. } } DataList1.DataBind(); }你可以参考sdk/msdn获得更多帮助
注意 可以在调用 Button 的任何步骤中使用 LinkButton 或 ImageButton。
为 DataList 控件创建 EditItemTemplate,它包括下列内容:
用户可更改的所有值的控件。例如,它包括所有字符和数值数据的 TextBox 控件。使用 DataBinding 属性设置这些控件的数据源。有关信息,请参见 Web 窗体页中的数据访问。
Text 属性为“更新”的 Button 控件,该控件的 CommandName 属性设置为 update(区分大小写)。
Text 属性为“取消”的 Button 控件,该控件的 CommandName 属性设置为 cancel(区分大小写)。
“更新”按钮将使用户得以指定他们已完成编辑。“取消”按钮将允许用户在不保存更改的情况下退出编辑。 为控件的 EditCommand 事件创建一个事件处理程序。有关详细信息,请参见在 Web 窗体页中创建事件处理程序。在事件处理程序中,执行下列操作:
将 DataList 控件的 EditItemIndex 属性设置为要转入编辑模式的项的索引值。用户所单击项的索引可以通过 Item 对象的 ItemIndex 属性获得。
将控件绑定到它的数据源。
该事件处理程序类似如下所示: ' Visual Basic
Private Sub DataList1_EditCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) _
Handles DataList1.EditCommand
DataList1.EditItemIndex = e.Item.ItemIndex
DataList1.DataBind()
End Sub// C#
private void DataList1_EditCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
DataList1.DataBind();
}
为 UpdateCommand 事件创建一个方法,该方法读取当前项中的编辑控件值,并将其写回到数据源。若要获取该项中特定控件的值,请使用 Item 事件参数对象的 Control.FindControl 方法。更新数据源后,通过将 EditItemIndex 属性设置为 -1,将该项切换出编辑模式,然后重新绑定到数据源。
安全说明 Web 窗体页中的用户输入可能包含潜在的恶意客户端脚本。默认情况下,Web 窗体页验证用户输入是否不包括脚本或 HTML 元素。有关更多信息,请参见脚本利用和在 Web 应用程序中防止脚本利用。
' Visual Basic
Private Sub DataList1_UpdateCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) _
Handles DataList1.UpdateCommand
Dim quantity As TextBox
quantity = CType(e.Item.FindControl("quantity"), TextBox)
Dim newQuantity As String = quantity.Text
' Add code here to update the Quantity value of the data source
' using the newQuantity variable. Note that newQuantity is a
' string and would have to be converted to a number.
DataList1.EditItemIndex = -1
DataList1.DataBind()
End Sub// C#
private void DataList1_UpdateCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
TextBox quantity;
quantity = (TextBox)(e.Item.FindControl("Quantity"));
string newQuantity = quantity.Text;
// Add code here to update the Quantity value of the data source
// using the newQuantity variable. Note taht newQuantity is a
// string and would have to be converted to a number.
DataList1.EditItemIndex = -1;
DataList1.DataBind();
}
为 CancelCommand 事件创建一个方法,该方法将 EditItemIndex 属性设置为 -1,然后重新绑定到数据源:
' Visual Basic
Private Sub DataList1_CancelCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) _
Handles DataList1.CancelCommand
' Switch out of edit mode.
DataList1.EditItemIndex = -1
DataList1.DataBind()
End Sub// C#
private void DataList1_CancelCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
// Switch out of edit mode.
DataList1.EditItemIndex = -1;
DataList1.DataBind();
}
为 DeleteCommand 事件创建事件处理程序。有关详细信息,请参见在 Web 窗体页中创建事件处理程序。在此方法中:
从数据源中删除项,然后重新绑定 DataList 控件。用户所单击项的索引可以通过 Item 对象的 ItemIndex 属性获得。若要获取单个控件的值,请使用 Item 事件参数对象的 FindControl 方法。
将控件重新绑定到它的数据源。
如果数据源是 DataTable 对象,则事件处理程序可能类似如下所示: ' Visual Basic
Private Sub DataList1_DeleteCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) _
Handles DataList1.DeleteCommand
' Code to delete the item from the data source.
Dim aTable As DataTable
aTable = CType(DataList1.DataSource, DataTable)
aTable.Rows(e.Item.ItemIndex).Delete()
' Bind the data after item is deleted.
DataList1.DataBind()
End Sub// C#
private void DataList1_DeleteCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
// Code to delete the item from the data source.
DataTable aTable = (DataTable)DataList1.DataSource;
aTable.Rows[e.Item.ItemIndex].Delete();
// Bind the data after the item is deleted.
DataList1.DataBind();
}
允许用户一次删除多个项 在 ItemTemplate(如果使用,还有 AlternatingItemTemplate)中,添加 CheckBox Web 服务器控件并将其 ID 属性设置为“删除”。请确保该 CheckBox 控件的 AutoPostBack 属性为 false。通过右击 DataList 控件并从菜单中选择“结束模板编辑”,关闭模板编辑。
将 Button Web 服务器控件添加到 Web 窗体页。将 Text 属性设置为“全部删除”,并将 ID 属性设置为 DeleteAll。此按钮不添加到 DataList 模板之一。
为“全部删除”按钮的 Click 事件创建方法。在方法中:
依次通过 DataList 控件的 Items 集合,按顺序提取每一项。
在项内,使用该项的 FindControl 方法获取步骤 1 的 CheckBox 控件,然后测试其 Checked 属性。
如果该框处于选中状态,则从数据源中删除相应的项。
将 DataList 控件重新绑定到它的数据源。
下面的示例显示 DeleteAll 按钮的事件处理程序,它使用上面所述的过程成批删除项。 ' Visual Basic
Private Sub DeleteAll_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles DeleteAll.Click
Dim isDeleted As Boolean
Dim anItem As DataListItem ' Check each box and see if the item should be deleted.
For Each anItem In DataList1.Items
isDeleted = CType(anItem.FindControl("Delete"), CheckBox).Checked
If isDeleted Then
' Add code here to delete the item, using anItem.ItemIndex.
End If
Next
DataList1.DataBind()
End Sub// C#
protected void DeleteAll_Click(object sender, System.EventArgs e)
{
bool isDeleted; // Check each box and see if the item should be deleted.
foreach (DataListItem anItem in DataList1.Items)
{
isDeleted =
((CheckBox)anItem.FindControl("Delete")).Checked;
if (isDeleted)
{
// Add code here to delete the item, using anItem.ItemIndex.
}
}
DataList1.DataBind();
}你可以参考sdk/msdn获得更多帮助
把那个private 改为public 或者 protected
再在OnEditCommand等事件中加入处理方法即可
已经解决了,特别要谢谢 hackate(~兰心*-*寒~)应该要区分大小写!!