【问题标题】:Using Galleria jQuery plugin with an asp.net ListView将 Galleria jQuery 插件与 asp.net ListView 一起使用
【发布时间】:2011-10-06 20:21:51
【问题描述】:

我正在尝试将 Galleria 与 asp.net ListView 一起使用,该列表视图在上传这些图像后从数据库中获取图像源。以下是我的列表视图:

        <div id="photoAlbumDiv" class="photoAlbumDiv">


        <asp:ListView ID="ListView1" runat="server" DataKeyNames="id" >
            <AlternatingItemTemplate>
                <td runat="server" style="">
                </td>
            </AlternatingItemTemplate>
            <EditItemTemplate>
                <td runat="server" style="">
                </td>
            </EditItemTemplate>
            <EmptyDataTemplate>
                <table style="">
                    <tr>
                        <td>
                            No data was returned.</td>
                    </tr>
                </table>
            </EmptyDataTemplate>
            <InsertItemTemplate>
                <td runat="server" style="">

                </td>
            </InsertItemTemplate>
            <ItemTemplate>
                <td runat="server" style="">
                    <img id="photoAlbumPhotos" src='<%# Eval("img") %>' alt="Image Not Found" class="photoAlbumPhotos" />
                </td>
            </ItemTemplate>
            <LayoutTemplate>
                <table runat="server" border="0" style="">
                    <tr ID="itemPlaceholderContainer" runat="server">
                        <td ID="itemPlaceholder" runat="server">
                        </td>
                    </tr>
                </table>
                <div style="">
                </div>
            </LayoutTemplate>
            <SelectedItemTemplate>
                <td runat="server" style="">
                    id:
                    <asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' />
                    <br />
                    img:
                    <asp:Label ID="imgLabel" runat="server" Text='<%# Eval("img") %>' />
                    <br />
                </td>
            </SelectedItemTemplate>
        </asp:ListView>

    </div>

这是我的 jquery:

                        <script type="text/javascript">
                        Galleria.loadTheme('Scripts/themes/classic/galleria.classic.min.js');
                        $("#photoAlbumDiv").galleria({
                            height: 1000,
                            width: 1000
                        });
                         </script>

可以吗,谢谢

【问题讨论】:

标签: jquery asp.net image listview


【解决方案1】:

您以错误的方式处理事情并使其变得比需要的更困难。 Galleria 插件期望 HTML 组织如下:

<div>
    <img />
    <img />
    <img />
    ...
    <img />
</div>

如您所见,div 中只有图像。您有一个不起作用的表结构。使用以下代码运行一个快速示例,您会发现它是多么简单。

Javascript

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.2.js"></script>
<script type="text/javascript" src="galleria/galleria-1.2.4.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        Galleria.loadTheme('galleria/themes/classic/galleria.classic.min.js');
        $("#gallery").galleria({
            width: 200,
            height: 300
        });
    });        
</script>

ASPX

<asp:ListView runat="server" ID="lvw">
    <LayoutTemplate>
        <div id="gallery">
            <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
        </div>
    </LayoutTemplate>
    <ItemTemplate>
        <img id="photoAlbumPhotos" src='<%# Eval("img") %>' alt="Image Not Found" class="photoAlbumPhotos" />
    </ItemTemplate>
</asp:ListView>

C#

protected void Page_Load(object sender, EventArgs e)
{
    lvw.DataSource = //Build datasource from database;
    lvw.DataBind();
}

就是这样。您应该在浏览器中运行一个简单的 Galleria 画廊。

【讨论】:

  • 但是使用列表视图的重点是从数据库中检索图像,如果我正确理解您的代码,我必须在后面的代码中指定每个图像
  • 我对图像 URL 进行了硬编码,因为它可以让您轻松地在自己的机器上运行示例代码,看看这个解决方案对您来说有多好。您可以轻松更改 C# 代码以使用存储在数据库中的数据绑定 ListView。我会更新问题,以便您了解我的意思。
  • @Wahtever:我稍微更新了答案,以表明您可以使用数据库中的数据数据绑定 ListView。如果您使用 SqlDataSource 或 ObjectDataSource 来绑定 ListView,那么此代码仍将完全按原样工作,但您无需进行代码隐藏。更新您的问题,让我知道您如何绑定 ListView。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多