2013-07-19 53 views
0

我有datalist包含圖像。在點擊數據列表中的圖像時獲取圖像src

我通過從數據庫中獲取圖像名稱將這些圖像綁定到datalist

我只想得到src [用戶點擊datalist的圖像路徑]。

我DataList控件是如下:

<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

我會去這樣的:

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>