how can I create an image gallery from a list of image URLs?

牧云@^-^@ 提交于 2019-12-11 06:38:34

问题


I'm able to get a bunch of image URLs using Amazon SimpleDB. I'm trying to understand the best way to bind the URL's to a Repeater and create a photo gallery. Repeater may not be the best Data control, so I'm open to suggestions if you can think of a better way.

List<string> imgURLS = new List<string>();    

String selectExpression = "Select * From Gallery Where Category = 'imgurls'";
SelectRequest selectRequestAction = new SelectRequest().WithSelectExpression(selectExpression);
SelectResponse selectResponse = sdb.Select(selectRequestAction);

if (selectResponse.IsSetSelectResult())
{
    SelectResult selectResult = selectResponse.SelectResult;
    foreach (Item item in selectResult.Item)
    {
        Console.WriteLine("  Item");
        if (item.IsSetName())
        {
           imgURLS.Add(item.Value)  //the URL of the image
        }
    }
}

 Repeater1.DataSource = imgURLS;
 Repeaster1.DataBind();

In this example, I just building a List[string] of the URLs, but all the examples I see online use an inline DataBinding SQL type function with an Eval type statement.

In the .aspx page, do I have to set any thing other than the ItemTemplate?

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
   //How do I direct my DataSource here?
    </ItemTemplate>
</asp:Repeater>

回答1:


You need to add two classes to your project in the App_Code directory.

One will contain a wrapper for the string class (I called it StringWrapper), and the other will contain a method of type List. This last method will return your imgURLS list.

public class StringWrapper
{
    public string Value
    { get; set; }
    public StringWrapper(string s)
    {
        this.Value = s;
    }

    public static implicit operator StringWrapper(string s)
    {
        return new StringWrapper(s);
    }
}

public static class Tools
{
    public static List<StringWrapper> GetImgUrls()
    {
        List<StringWrapper> imgURLS = new List<StringWrapper>();    

        String selectExpression = "Select * From Gallery Where Category = 'imgurls'";
        SelectRequest selectRequestAction = new SelectRequest().WithSelectExpression(selectExpression);
        SelectResponse selectResponse = sdb.Select(selectRequestAction);

        if (selectResponse.IsSetSelectResult())
        {
            SelectResult selectResult = selectResponse.SelectResult;
            foreach (Item item in selectResult.Item)
            {
                Console.WriteLine("  Item");
                if (item.IsSetName())
                {
                   imgURLS.Add(item.Value)  //the URL of the image
                }
            }
        }
        return imgURLS;
    }
}

Then in design mode on your aspx page, you select the repeater and click the top right corner. You click chose datasource, add new datasource. You select object, (rename it if you want) and click ok.

Then you uncheck the checkbox to see all objects that can be used, you chose the name of the class you created (here, Tools). You click next, then you select the GetImgUrls Method and click terminate.

Then to use it, just call <%# Eval("Value") %> in your ItemTemplate, for example:

    <ItemTemplate>
        <img src='<%# Eval("Value") %>' />
    </ItemTemplate>

The Eval function looks for properties, and a string has no property except the "Length" property. That is why you need to make a stringwrapper, so that Eval can call the Value property and obtain the string value.



来源:https://stackoverflow.com/questions/7828800/how-can-i-create-an-image-gallery-from-a-list-of-image-urls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!