ItemCommand Event (ObjectList)
Occurs when the user selects a command that is associated with an ObjectList item.
public event ObjectListCommandEventHandler ItemCommand
Remarks
When an ItemCommand event handler is defined, the ObjectList control notifies the handler when an item event is generated through user interaction.
When you render an object list by using templates, the ItemCommand event handler is called through the event-bubbling mechanism of ASP.NET. The event handler is passed an argument of typeObjectListCommandEventArgs, which contains information about the source item and the CommandName property of the control that generated the event. This allows you to render a single item with multiple associated interactions.
On default rendering, the control renders each of the commands defined for the object list as a link. 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. The value in the CommandName property is the name of the command that the user invoked.
Example
The following example demonstrates how to trap the ItemCommand event. The CommandName property specifies what action to perform.
<Script language="vb" runat="server">
Dim Command1 As System.Web.UI.MobileControls.ObjectListCommand
Dim Command2 As System.Web.UI.MobileControls.ObjectListCommand
Class Book
Private _BookName, _Author, _InStock As String
Public Sub New(BookName As String, Author As String, InStock As String)
_BookName = BookName
_Author = Author
_InStock = InStock
End Sub 'New
Public ReadOnly Property BookName() As String
Get
Return _BookName
End Get
End Property
Public ReadOnly Property Author() As String
Get
Return _Author
End Get
End Property
Public ReadOnly Property InStock() As String
Get
Return _InStock
End Get
End Property
End Class
Sub SelectCommand(sender As Object, e As ObjectListShowCommandsEventArgs)
If CType(e.ListItem.DataItem, Book).InStock = "Yes" Then
ObjectList1.Commands.Add(Command1)
Else
ObjectList1.Commands.Add(Command2)
End If
End Sub
Sub AskForBook(sender As Object, e As ObjectListCommandEventArgs)
' Selected ObjectListCommand from ObjectListCommandCollection.
' You can reference it and perform an action on it depending
' on the value of the CommandName property.
If e.CommandName = "CheckOut" Then
ActiveForm = Form2
Else
If e.CommandName = "Reserve" Then
ActiveForm = Form3
End If
End If
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Create and fill an array.
Dim arr As New ArrayList()
arr.Add(New Book("Sushi, Anyone?", "O'Leary", "Yes"))
arr.Add(New Book("Secrets of Silicon Valley", "Dull", "No"))
arr.Add(New Book("Net Etiquette", "Locksley", "Yes"))
arr.Add(New Book("The Gourmet Microwave", "Ringer", "No"))
' Associate and bind array to the ObjectList object.
ObjectList1.DataSource = arr
ObjectList1.DataBind()
' Override autogeneration.
ObjectList1.LabelField = "BookName"
Command1 = New System.Web.UI.MobileControls.ObjectListCommand("CheckOut", "CheckOut")
Command2 = New System.Web.UI.MobileControls.ObjectListCommand("Reserve", "Reserve")
End Sub
</Script>
<mobile:Form runat="server" id="Form1" >
<mobile:ObjectList id="ObjectList1" runat="server" OnItemCommand="AskForBook" OnShowItemCommands="SelectCommand">
</mobile:ObjectList>
<mobile:Label id="Label1" runat="server"></mobile:Label>
</mobile:Form>
<mobile:Form id="Form3" runat="server">
<mobile:Label id="Label2" runat="server">Placed On Reserve!</mobile:Label>
</mobile:Form>
<mobile:Form id="Form2" runat="server">
<mobile:Label id="Label3" runat="server">Successfully Checked Out!</mobile:Label>
</mobile:Form>
[C#]
<script Language="c#" runat="server">
System.Web.UI.MobileControls.ObjectListCommand Command1;
System.Web.UI.MobileControls.ObjectListCommand Command2;
class Book
{
private String _BookName, _Author, _InStock;
public Book(String BookName, String Author, String InStock)
{
_BookName = BookName;
_Author = Author;
_InStock = InStock;
}
public String BookName { get { return _BookName; } }
public String Author { get { return _Author; } }
public String InStock { get { return _InStock; } }
}
public void Page_Load(Object sender, EventArgs e)
{
// Create and fill an array.
ArrayList arr = new ArrayList();
arr.Add (new Book ("Sushi, Anyone?", "O'Leary", "Yes"));
arr.Add (new Book ("Secrets of Silicon Valley", "Dull", "No"));
arr.Add (new Book ("Net Etiquette", "Locksley", "Yes"));
arr.Add (new Book ("The Gourmet Microwave", "Ringer", "No"));
// Associate and bind array to the ObjectList object.
ObjectList1.DataSource = arr;
ObjectList1.DataBind ();
// Override autogeneration.
ObjectList1.LabelField = "BookName";
Command1 = new System.Web.UI.MobileControls.ObjectListCommand
("CheckOut", "CheckOut");
Command2 = new System.Web.UI.MobileControls.ObjectListCommand
("Reserve", "Reserve");
}
void SelectCommand(Object sender,
ObjectListShowCommandsEventArgs e)
{
if ( ((Book)e.ListItem.DataItem).InStock == "Yes" )
{
ObjectList1.Commands.Add(Command1);
}
else
{
ObjectList1.Commands.Add(Command2);
}
}
void AskForBook(Object sender, ObjectListCommandEventArgs e)
{
// Selected ObjectListCommand from ObjectListCommandCollection.
// You can reference it and perform an action on it depending
// on the value of the CommandName property.
if (e.CommandName == "CheckOut")
{
ActiveForm = Form2;
}
else if (e.CommandName == "Reserve")
{
ActiveForm = Form3;
}
}
</script>
<mobile:Form runat="server" id="Form1" >
<mobile:ObjectList id="ObjectList1" runat="server" OnItemCommand="AskForBook" OnShowItemCommands="SelectCommand">
</mobile:ObjectList>
<mobile:Label id="Label1" runat="server"></mobile:Label>
</mobile:Form>
<mobile:Form id="Form3" runat="server">
<mobile:Label id="Label2" runat="server">Placed On Reserve!</mobile:Label>
</mobile:Form>
<mobile:Form id="Form2" runat="server">
<mobile:Label id="Label3" runat="server">Successfully Checked Out!</mobile:Label>
</mobile:Form>
See Also
Command Class | CommandEventArgs Class (Command) | ItemCommand Event (List) | ListCommandEventArgs Class | OnItemCommand Method | ObjectList Class