ObjectList.ItemCommand Zdarzenie

Definicja

Występuje, gdy użytkownik wybierze polecenie skojarzone z elementem ObjectList.Occurs when the user selects a command that is associated with an ObjectList item. {1>Ten interfejs API jest nieaktualny.<1}This API is obsolete. Informacje o sposobach tworzenia aplikacji mobilnych ASP.NET można znaleźć w temacie Mobile Apps & witryny z ASP.NET.For information about how to develop ASP.NET mobile applications, see Mobile Apps & Sites with ASP.NET.

public:
 event System::Web::UI::MobileControls::ObjectListCommandEventHandler ^ ItemCommand;
public event System.Web.UI.MobileControls.ObjectListCommandEventHandler ItemCommand;
member this.ItemCommand : System.Web.UI.MobileControls.ObjectListCommandEventHandler 
Public Custom Event ItemCommand As ObjectListCommandEventHandler 

Przykłady

Poniższy przykład kodu demonstruje sposób zalewkowania zdarzenia ItemCommand.The following code example demonstrates how to trap the ItemCommand event. Właściwość CommandName określa, jaką akcję wykonać.The CommandName property specifies what action to perform.

Uwaga

Poniższy przykład kodu używa modelu kodu jednoplikowego i może nie działa poprawnie, jeśli jest kopiowany bezpośrednio do pliku związanego z kodem.The following code sample uses the single-file code model and may not work correctly if copied directly into a code-behind file. Ten przykład kodu musi być skopiowany do pustego pliku tekstowego, który ma rozszerzenie. aspx.This code sample must be copied into an empty text file that has an .aspx extension. Aby uzyskać więcej informacji, zobacz model kodu stron ASP.NET Web Forms.For more information, see ASP.NET Web Forms Page Code Model.

<%@ 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>

Uwagi

Po zdefiniowaniu programu obsługi zdarzeń ItemCommand, ObjectList powiadamia program obsługi po wygenerowaniu zdarzenia elementu przez interakcję z użytkownikiem.When an ItemCommand event handler is defined, the ObjectList notifies the handler when an item event is generated through user interaction.

Po wyrenderowaniu listy obiektów przy użyciu szablonów program obsługi zdarzeń ItemCommand jest wywoływany za pośrednictwem mechanizmu propagacji zdarzeń ASP.NET.When you render an object list by using templates, the ItemCommand event handler is called through the event-bubbling mechanism of ASP.NET. Program obsługi zdarzeń przekazuje argument typu ObjectListCommandEventArgs, który zawiera informacje o elemencie źródłowym i właściwości CommandName formantu, który wygenerował zdarzenie.The event handler is passed an argument of type ObjectListCommandEventArgs, which contains information about the source item and the CommandName property of the control that generated the event. Dzięki temu można renderować pojedynczy element z wieloma skojarzonymi interakcjami.This allows you to render a single item with multiple associated interactions.

W przypadku domyślnego renderowania formant renderuje każde polecenie zdefiniowane dla listy obiektów jako łącze.On default rendering, the control renders each of the commands defined for the object list as a link. Gdy użytkownik kliknie łącze, program obsługi zdarzeń ItemCommand jest wywoływany z argumentem typu ObjectListCommandEventArgs, który zawiera informacje o elemencie źródłowym.When the user clicks a link, the ItemCommand event handler is called with an argument of type ObjectListCommandEventArgs, which contains information about the source item. Wartość we właściwości CommandName jest nazwą polecenia wywoływanego przez użytkownika.The value in the CommandName property is the name of the command that the user invoked.

Dotyczy

Zobacz też