首页 新闻 会员 周边

关于asp.net Repeater控件和JS脚本的一段代码

0
悬赏园豆:10 [已解决问题] 解决于 2012-10-10 11:02

这段代码我看不太懂,麻烦详细解释下JS脚本和Repeater控件那一块

 

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
   
    void Page_Load()
    {
        if (!Page.IsPostBack)
        {
            DirectoryInfo dir = new DirectoryInfo(MapPath("~/Photos"));
            rptPhotos.DataSource = dir.GetFiles("*.jpg");
            rptPhotos.DataBind();
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <style type="text/css">
        .photo
        {
            width:400px;
            background-color:white;
            filter:progid:DXImageTransform.Microsoft.Fade(duration=2);
        }
    </style>
    <script type="text/javascript">
    var photos = new Array();
    window.setInterval(showImage, 2000);
   
    function showImage()
    {
        if (photos.length > 0)
        {
            var index = Math.floor(Math.random() * photos.length);
            var image = document.getElementById('imgPhoto');
            image.src = photos[index];
            if (image.filters)
            {
                image.filters[0].Apply();
                image.filters[0].Play();
            }
        }
    }       
    </script>
    <title>Show Repeater Photos</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>


    <img id="imgPhoto" alt="" class="photo" />
    <script type="text/javascript">
    <asp:Repeater
        id="rptPhotos"
        Runat="server">
        <ItemTemplate>
        <%# Eval("Name", "photos.push('Photos/{0}')") %>
        </ItemTemplate>
    </asp:Repeater>
    showImage();
    </script>
   
    </div>
    </form>
</body>
</html>

微澜的主页 微澜 | 初学一级 | 园豆:-1
提问于:2012-09-26 19:19
< >
分享
最佳答案
0

repeater是将读取到的图片数据添加到photos数组。用的是Array.push方法。

JS中的showImage方法似乎是随机显示photos中的图片。

收获园豆:10
I,Robot | 大侠五级 |园豆:9783 | 2012-09-26 20:27

对,这就是显示随机照片的,repeater可以控制JS的函数么?

微澜 | 园豆:-1 (初学一级) | 2012-09-27 18:31

@微澜: 

这段代码里的repeater实际上就是输出一段js代码,这段js代码的功能就是将图片地址加入photos数组中。

你运行代码看看最终的页面代码就明白了。

I,Robot | 园豆:9783 (大侠五级) | 2012-09-27 20:06
其他回答(1)
0

photos 空数组 有意义么

程序员的人生 | 园豆:235 (菜鸟二级) | 2012-09-27 10:57
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册