|
-
Jan 21st, 2008, 05:28 PM
#2
Re: [2005] Want to display several images from DB
The GetImages page from the previous thread writes directly to the Response stream. It is not a Web Page as you would normally think of one. IE it doesn't have any controls/GUI, think of it more as calling a Function.
If your website has a page called ShowImages the ImageURL properties of all ASP Image Controls would call the GetImages page. For example, if you had 3 image controls, the ImageURL properties might be ~/GetImages.aspx?imageid=1, ~/GetImages.aspx?imageid=2, ~/GetImages.aspx?imageid=3.
However, it is only until ShowImages.aspx is being downloaded does the web browser call the GetImages.aspx page. In the above scenario it is called 3 times.
In order to use GetImages with a Repeater or DataList in the ShowImages page, you could create a datasource that contains just a list of Student id's. Once the DataBind method is called the ItemCreated and ItemDataBound events are fired for each Id in the list. At this point ASP.NET dynamically creates and adds the controls you have specified in then ItemTemplate section. In the ItemDataBound method you get a reference to this control and then set the ImageURL property accordingly.
Code:
'ShowImages.aspx.vb code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'load some student ids
Dim pics As New System.Collections.ArrayList
pics.Add(1)
pics.Add(2)
pics.Add(3)
Repeater1.DataSource = pics
Repeater1.DataBind()
End Sub
Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
Dim img As System.Web.UI.WebControls.Image
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
img = DirectCast(e.Item.FindControl("image1"), System.Web.UI.WebControls.Image)
img.ImageUrl = "~/GetImages.aspx?imageid=" & e.Item.DataItem.ToString
End If
End Sub
'ShowImages.aspx code
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" />
</ItemTemplate>
</asp:Repeater>
Hope this helps (and makes some sense).
Last edited by brucevde; Jan 21st, 2008 at 05:34 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|