我会这样做:
Protected Sub ImageButton1_Click(sender As Object, e As ImageClickEventArgs)
Dim btn As ImageButton = sender
Dim selImage As String = btn.ImageUrl.ToString
MsgBox(btn.ImageUrl.ToString)
'Shorter:
Dim selImage As String = sender.ImageUrl.ToString
End Sub
完整的测试代码:
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim ditems As New List(Of di)
For i As Integer = 0 To 10
Dim dii As New di
dii.d_Name = "x" & i
ditems.Add(dii)
Next
dlImages.DataSource = ditems
dlImages.DataBind()
End If
End Sub
Protected Sub dlImages_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlImages.ItemDataBound
If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.EditItem Then
CType(e.Item.FindControl("ImageButton1"), ImageButton).ImageUrl = "~/ScreenMasterImages/" & DirectCast(e.Item.DataItem, di).d_Name & ".jpg"
End If
End Sub
Protected Sub ImageButton1_Click(sender As Object, e As ImageClickEventArgs)
Dim btn As ImageButton = sender
Dim selImage As String = btn.ImageUrl.ToString
MsgBox(btn.ImageUrl.ToString)
End Sub
结束类
Public Class di
Public Property d_Name() As String
Get
Return m_d_Name
End Get
Set(value As String)
m_d_Name = value
End Set
End Property
Private m_d_Name As String
结束类
和asp:
<asp:DataList ID="dlImages" runat="server" DataKeyField="d_Name" DataMember="d_Name" RepeatColumns="4">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" OnClick="ImageButton1_Click" Height="200px" Width="200px" runat="server" />
</ItemTemplate>
</asp:DataList>