Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2005
    Posts
    63
    Thanks
    1
    Thanked 0 Times in 0 Posts

    ASP:Repeater help

    Currently, i am using a asp:repeater to bind my images from database to a imagebutton. For example, i have 2 images in the database. Therefore, the repeater should have generate 2 imagebuttons for me. And each imagebutton is suppose to show 2 different images.

    Somehow both imagebuttons are displaying the same images, which always happens to be the 2nd image. Before the itemdatabound, i already databind it to an arraylist. The imgRetrieve.aspx is used to retrieve/display image for me based on the id passed in. Any help is greatly appreciated.

    Code:
    <asp:repeater id="repImages" runat="server" OnItemDataBound="repImages_ItemDataBound">
    		<ItemTemplate>
    			<asp:ImageButton runat="server" ID="imgItem" CssClass="removeBorder" onmouseover="this.className='applyBorder'"
    				onmouseout="this.className='removeBorder'" Width="100" Height="100" OnClick="imgItem_Click" />
    			<br>
    		</ItemTemplate>
    	</asp:repeater>
    Code:
        Protected Sub repImages_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles repImages.ItemDataBound
    
    Dim Item As RepeaterItem = CType(e.Item, RepeaterItem)
            Dim MyImage As New ImageButton
    MyImage = CType(Item.FindControl("imgItem"), ImageButton)
    dim index as integer=0
    for index=0 to imgArrList.count-1
    MyImage.ImageUrl = "..\imgRetrieve.aspx?id=" & imgArrList.item(index).staffid
    end next

  • #2
    New to the CF scene
    Join Date
    Mar 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think you might be overthinking it a bit. When using a repeater, all you should have to do is to bind it to a dataset of the items from your DB.
    For example (emphasis on the ImageUrl):
    Code:
    <asp:repeater id="repImages" runat="server" >
         <ItemTemplate>
              <asp:ImageButton runat="server" ID="imgItem" CssClass="removeBorder" 
                   onmouseover="this.className='applyBorder'"
                   onmouseout="this.className='removeBorder'" 
                   Width="100" Height="100" OnClick="imgItem_Click" 
                   ImageUrl='<%# container.dataitem("rowname") %>'
              </asp:ImageButton>
    	  <br>
         </ItemTemplate>
    </asp:repeater>
    Then within your code:

    Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If Not IsPostBack Then
                'instantiate DB routines
                Dim DB As New SQL_Calls
    
                'Call DB routines to READ table, returning Dataset of all rows
                repImages.DataSource = DB.ReadTbl
    
                'Bind the Dataset to the Repeater
                repImages.DataBind()
            End If
        End Sub
    Then within the SQL_Calls.ReadTbl you define your SQL. I think what's happening with your code is that you have your repeater bound outside the code that you included. For EACH row that it gets, it runs through your repImages_ItemDataBound. This would cause each row to loop through the FOR loop imgArrList.count times and the last image would be attached to EACH row.

    Does that make sense?
    Kev


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •