OnItemCommand 当在ItemTemplate 中所宣告的Button 或LinkButton 控件触发事件时,如果该控件的CommandName 属性内容不是Edit、Cancel、Delete 或Update 时,便触发本事件。其宣告语法为:
Sub OnItemCommand(Sender As Object, e As DataListCommandEventArgs)
...
End Sub
其中参数e 有2 个属性,如下表所示:
要使用本事件前,首先必须在ItemTemplate 中宣告LinkButton 或Button Web 控件。下列范例在ItemTemplate 中宣告一个LinkButton Web 控件,当使用者按下LinkButton Web 控件后,会显示该项目的详细数据:
<%@Import Namespace=System.Data.ADO%>
<%@Import Namespace=System.Data%>
<!--#Include File="GetTable.inc"-->
<Html>
<Form runat="Server">
<ASP:DataList Id="dlA" OnItemCommand="dlA_ICMD" GrIdLines="both"
Runat="Server">
<Template Name="ItemTemplate">
<ASP:Image ImageUrl="ico1.gif" Runat="Server"/>
姓名: <%#Container.DataItem("UserName")%>
<ASP:LinkButton Id="lbShow" Text=">" Runat="Server"/>
</Template>
<Template Name="SelectedItemTemplate">
<ASP:Image ImageUrl="ico1.gif" Runat="Server"/>
姓名: <%#Container.DataItem("UserName")%>
<ASP:LinkButton Id="lbClose" Text="<" Runat="Server"/><br>
电话: <%#Container.DataItem("UserTel")%><br>
住址: <%#Container.DataItem("UserAdd")%><br>
电邮: <%#Container.DataItem("UserEmail")%><br>
</Template>
</ASP:DataList>
</Form>
<Script Language="VB" Runat="Server">
Sub Page_Load(Sender As Object, e As EventArgs)
Dim dtDataTable As DataTable=GetTable("CH05\MyWeb.mdb", "Members")
dlA.DataSource=dtDataTable.DefaultView
Page.DataBind()
End Sub
Sub dlA_ICMD(Sender As Object, e As DataListCommandEventArgs)
If e.CommandSource.Id="lbShow" Then
dlA.SelectedIndex=e.Item.ItemIndex
ElseIf e.CommandSource.Id="lbClose"
dlA.SelectedIndex=-1
End If
dlA.DataBind()
End Sub
</SCRIPT>
</Html>