获取图像src点击数据列表中的图像

时间:2013-07-19 04:56:49

标签: asp.net .net vb.net visual-studio-2008

我有datalist包含图片。

我通过从数据库中获取图像名称将这些图像限制在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。

1 个答案:

答案 0 :(得分: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>