ObjectList.ShowItemCommands 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在与 ObjectList 中的项相关的命令显示之前发生。 此 API 已废弃不用。 有关如何开发 ASP.NET 移动应用程序的信息,请参阅 移动应用 & 具有 ASP.NET 的网站。
public:
event System::Web::UI::MobileControls::ObjectListShowCommandsEventHandler ^ ShowItemCommands;
public event System.Web.UI.MobileControls.ObjectListShowCommandsEventHandler ShowItemCommands;
member this.ShowItemCommands : System.Web.UI.MobileControls.ObjectListShowCommandsEventHandler
Public Custom Event ShowItemCommands As ObjectListShowCommandsEventHandler
事件类型
示例
下面的代码示例演示如何使用 ShowItemCommands 事件从 ObjectListCommandCollection 的详细信息视图的 中删除项 ObjectList。 在列表视图中选中要删除的项。
注意
下面的代码示例使用单文件代码模型,如果直接复制到代码隐藏文件中,可能无法正常工作。 此代码示例必须复制到扩展名为 .aspx 的空文本文件中。 有关详细信息,请参阅 ASP.NET Web 窗体页代码模型。
<%@ Page Language="C#"
Inherits="System.Web.UI.MobileControls.MobilePage" %>
<%@ Register TagPrefix="mobile"
Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<script runat="server">
//System.Web.UI.MobileControls.ObjectListItem item;
//System.Web.UI.MobileControls.ObjectListItemCollection itemColl;
// Get the persisted array through postbacks.
ArrayList arr = new ArrayList();
public void Page_Load(Object sender, EventArgs e)
{
if (!IsPostBack)
{
// Create and fill the array
arr.Add(new Task("Tomorrow's work", "Yes", 1));
arr.Add(new Task("Today's work", "Yes", 1));
arr.Add(new Task("Yesterday's work", "No", 1));
// Persist the array in the Session object
Session["MyArrayList"] = arr;
// Associate and bind array to the
// ObjectList for each postback.
ObjectList1.DataSource = arr;
ObjectList1.LabelField = "TaskName";
ObjectList1.DataBind();
}
}
private void ItemCommand_Click(Object sender,
ObjectListCommandEventArgs e)
{
// Get the array from the Session object
arr = (ArrayList)Session["MyArrayList"];
// Remove selected item from the ObjectLis
int i = ObjectList1.SelectedIndex;
arr.RemoveAt(i);
Session["MyArrayList"] = arr;
// Re-Bind ObjectList to altered ArrayList.
ObjectList1.DataSource = arr;
ObjectList1.LabelField = "TaskName";
ObjectList1.DataBind();
ObjectList1.ViewMode = ObjectListViewMode.List;
}
void ItemCommands_Show(Object sender,
ObjectListShowCommandsEventArgs e)
{
// Check conditions, and add or remove
// commands in the detail view.
if (e.ListItem["Editable"].Equals("No"))
ObjectList1.Commands.RemoveAt(0);
else if (ObjectList1.Commands.Count < 1)
ObjectList1.Commands.Add(new
ObjectListCommand("Delete", "Delete"));
}
private class Task
{
private string _TaskName;
private string _Editable;
private int _Days;
public Task(string TaskName, string Editable, int Days)
{
_TaskName = TaskName;
_Editable = Editable;
_Days = Days;
}
public string TaskName
{ get { return _TaskName; } }
public string Editable
{ get { return _Editable; } }
public int Days
{ get { return _Days; } }
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<mobile:Form runat="server" id="Form1" >
<mobile:ObjectList runat="server" id="ObjectList1"
OnItemCommand="ItemCommand_Click"
OnShowItemCommands="ItemCommands_Show" >
<Command Name="Delete" Text="Delete" />
</mobile:ObjectList>
<mobile:Label runat="server" id="Label1" />
<mobile:Label runat="server" id="Label2" />
</mobile:Form>
</body>
</html>
<%@ Page Language="VB"
Inherits="System.Web.UI.MobileControls.MobilePage" %>
<%@ Register TagPrefix="mobile"
Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<script runat="server">
' System.Web.UI.MobileControls.ObjectListItem item
' System.Web.UI.MobileControls.ObjectListItemCollection itemColl
' Get the persisted array through postbacks.
Private arr As New ArrayList()
Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
' Create and fill the array
arr.Add(new Task("Tomorrow's work", "Yes", 1))
arr.Add(new Task("Today's work", "Yes", 1))
arr.Add(new Task("Yesterday's work", "No", 1))
' Persist the array in the Session object
Session("MyArrayList") = arr
' Associate and bind array to the
' ObjectList for each postback.
ObjectList1.DataSource = arr
ObjectList1.LabelField = "TaskName"
ObjectList1.DataBind()
End If
End Sub
Private Sub SelectCommand(ByVal sender As Object, _
ByVal e As ObjectListCommandEventArgs)
' Get the array from the Session object
arr = CType(Session("MyArrayList"), ArrayList)
' Remove selected item from the ObjectLis
Dim i As Integer = ObjectList1.SelectedIndex
arr.RemoveAt(i)
Session("MyArrayList") = arr
' Re-Bind ObjectList to altered ArrayList.
ObjectList1.DataSource = arr
ObjectList1.LabelField = "TaskName"
ObjectList1.DataBind()
ObjectList1.ViewMode = ObjectListViewMode.List
End Sub
Public Sub ShowTaskDetail(ByVal sender As Object, _
ByVal e As ObjectListShowCommandsEventArgs)
' Check conditions, and add or remove
' commands in the detail view.
If e.ListItem("Editable").Equals("No") Then
ObjectList1.Commands.RemoveAt(0)
ElseIf ObjectList1.Commands.Count < 1 Then
ObjectList1.Commands.Add(New ObjectListCommand("Delete", "Delete"))
End If
End Sub
Private Class Task
Private _TaskName As String
Private _Editable As String
Private _Days As Integer
Public Sub New(ByVal TaskName As String, _
ByVal Editable As String, ByVal Days As Integer)
_TaskName = TaskName
_Editable = Editable
_Days = Days
End Sub
Public ReadOnly Property TaskName() As String
Get
Return _TaskName
End Get
End Property
Public ReadOnly Property Editable() As String
Get
Return _Editable
End Get
End Property
Public ReadOnly Property Days() As Integer
Get
Return _Days
End Get
End Property
End Class
</script>
<html xmlns="http:'www.w3.org/1999/xhtml" >
<body>
<mobile:Form runat="server" id="Form1" >
<mobile:ObjectList runat="server" id="ObjectList1"
OnItemCommand="SelectCommand" OnShowItemCommands="ShowTaskDetail" >
<Command Name="Delete" Text="Delete" />
</mobile:ObjectList>
<mobile:Label runat="server" id="Label1" />
<mobile:Label runat="server" id="Label2" />
</mobile:Form>
</body>
</html>
注解
事件参数包含对项和命令集合的引用。 事件处理程序可以根据项自定义集合。 应用程序可以使用 ShowItemCommands 事件提供与特定于项的快捷菜单等效的功能。 在此事件期间对命令集合所做的更改不会持久保存。
在调用 事件后调用此 ItemSelect 事件。 在显示详细信息视图之前,可以使用此事件执行其他任务。 可以添加、删除或重新排列字段,具体取决于列表视图中选定的项。