0

我有datalist包含图像。

datalist通过从数据库中获取图像名称来限制这些图像。

我只想获取src用户单击数据列表的[图像路径]。

我的数据列表如下:

    <asp:DataList ID="dlImages" runat="server" RepeatColumns="4">
        <ItemTemplate>
           <asp:ImageButton ID="ImageButton1" Height="200px" Width="200px"  runat="server" />
        </ItemTemplate>
    </asp:DataList>

我将其限定如下:

   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/" & e.Item.DataItem("PageName") & ".jpg"
            End If
        End Sub

请帮助我获取用户单击数据源的特定图像的 src。

4

1 回答 1

1

我会这样做:

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>
于 2013-08-22T09:01:39.010 回答