Results 1 to 19 of 19

Thread: [2005] Want to display several images from DB

Hybrid View

  1. #1
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    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.

  2. #2

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [2005] Want to display several images from DB

    Yes - that makes some sense (80% maybe )

    How does what you describe use the GetStuPhoto_T stored proc?

    Could I at least return TOP 3 ... Where STUID>lastid processed so that I visit the DB less often in the getimages.aspx?

    And does my showing the student name under the image control also make sense with what you have described?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Want to display several images from DB

    Quote Originally Posted by szlamany
    How does what you describe use the GetStuPhoto_T stored proc?
    It doesn't, not directly. ShowImages.aspx will have a repeater on it. It will call a new stored procedure that does something like

    SELECT TOP 3 ImageID FROM Students

    You then bind the repeater control. The repeater control contains an image control. For each row returned from the new stored procedure, one instance of the image control will be rendered to the page. You will specify the URL of the images for each instance by looking at the ID and doing something like

    theImg.ImageUrl = "GetImage.aspx?imageid=" & intImageID.ToString()


    Could I at least return TOP 3 ... Where STUID>lastid processed so that I visit the DB less often in the getimages.aspx?
    Unfortunately, no. This is a 'side effect' of storing images in a database, when it comes to displaying it, you can do one call at a time. So if you show 20 images, there will be 20 calls. However, you're in a little luck, because there is something known as data caching - in GetImage.aspx you can cache the byte array you got from the database for say 2 hours so that every request to a given ID gives that correct Byte Array. That's for afterwards, you need to do the repeater right now.


    And does my showing the student name under the image control also make sense with what you have described?
    Modify your new stored procedure to return the id and the student name. In the Repeater control's ItemDataBound event, you can write anything you want to each instance.

Posting Permissions

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



Click Here to Expand Forum to Full Width