如何在asp.net中动态显示图像

时间:2011-03-24 10:21:20

标签: asp.net

我有一个由多个图像组成的文件夹。最初必须从页面中的该文件夹加载第一个图像。当用户点击第一个图像时,它应该显示第二个图像,它将具有第三个图像的下一个链接。当用户现在点击下一个链接时,它应该显示第三个图像,它将具有第四个图像的下一个链接和第三个图像的上一个链接,它应该重复到图像的末尾。

任何人都可以告诉我如何通过将查询字符串值传递给图像以及如何动态更改图像链接来逐个显示图像

更新

最初我想加载第一个文件而不传递查询字符串,第一个文件应该有下一个文件链接。单击下一个链接按钮后,只需传递查询字符串。

如何将查询字符串从第二个文件传递到文件末尾,

    DirectoryInfo oImageFolderInfo = new DirectoryInfo(this.ImagePath);
    FileInfo[] oFileList = oImageFolderInfo.GetFiles("*.*");


    string fileName=string.Empty;

   if (Request.QueryString["filename"] != null)
    {
        fileName=(Request.QueryString["filename"]);

    }

    else
    {
        fileName = oFileList[0].Name;
    }

    HtmlImage imag = new HtmlImage();
    imag.Src = Url + fileName;


    HtmlAnchor nextAnchor = new HtmlAnchor();
   nextAnchor.Href=??
    nextAnchor.InnerText = "Next>>";

    HtmlAnchor prevAnchor = new HtmlAnchor();
    prevAnchor.Href=??

如何继续这样做到达文件的末尾?

2 个答案:

答案 0 :(得分:1)

执行此操作的一种方法是将图像和链接放在更新面板中,并使链接触发更新面板。在服务器上,抓住点击链接,只需更新图像源和链接。

另一种方法是使用javascript,最初创建一个图像源数组,并通过链接点击从该数组中检索(即在表单加载时执行Page.ClientScript.RegisterStartupScript())。

很抱歉,如果答案是通用的,但问题相当含糊。

<强>更新

        DirectoryInfo oImageFolderInfo = new DirectoryInfo(this.ImagePath);
        FileInfo[] oFileList = oImageFolderInfo.GetFiles("*.*"); 

        string currentFileName, nextFileName, prevFileName = string.Empty;
        int currentFileId, nextFileId = 0;
        int prevFileId = oFileList.Length - 1;

        /*not sure why you want the user to see the image name
        it is not really needed for the code
        Request.QueryString["filename"] != null && Request.QueryString["id"] != null*/
        if (Request.QueryString["id"] != null)
        {
            //currentFileName=(Request.QueryString["filename"]);
            if (!int.TryParse(Request.QueryString["id"],out currentFileId))
            {
                currentFileId = 0;
            }
            else if (currentFileId > 0)
            {
                prevFileId = currentFileId - 1;
            }

            if (currentFileId + 1 < oFileList.Length)
            {
                nextFileId = currentFileId + 1;
            }
        }
        else
        {
            if (oFileList.Length > 1)
            {
                nextFileName = oFileList[1].Name;
                nextFileId = 1;
            }       
        }

        currentFileName = oFileList[currentFileId].Name;
        nextFileName = oFileList[nextFileId].Name;
        prevFileName = oFileList[prevFileId].Name;

        /*Not sure what you are doing here ... you should already have an image
        container on the page and should be setting its source, 
        not creating a new image
        Same goes for the links (Anchors)*/
        HtmlImage imag = new HtmlImage();
        imag.Src = Url + currentFileName;
        HtmlAnchor nextAnchor = new HtmlAnchor();
        nextAnchor.Href="?filename=" + nextFileName + "&id=" + nextFileId;
        nextAnchor.InnerText = "Next>>";
        HtmlAnchor prevAnchor = new HtmlAnchor();
        prevAnchor.Href = "?filename" + prevFileName + "&id=" + prevFileId;

我没有测试此代码,但我认为它应该可行。您应该进行其他检查以查看目录中是否有文件等。此外,这不是一种非常有效的处理方式,因为您总是从目录中读取文件(您可以一次读取它们并将它们存储在一个会话变量(例如Session [“imageFiles”] = oFileList;)然后从Session中获取数组(例如oFileList = Session [“imageFiles”];)

答案 1 :(得分:0)

这不是表现最佳的方法,但很简单。

在您的代码隐藏中,在Page_Load上使用文件系统(System.IO)遍历目录中的所有文件。如果您需要以特定顺序显示图像,可以将它们命名为正确排序。将图像的ImageUrl设置为第一个文件。单击“下一步”按钮后,页面将再次加载。在Page_Load中,读取图像的ImageUrl,然后再次循环浏览文件,直到您超过最后一个图像。将图像的ImageUrl设置为下一个文件。

Dima的答案要好得多,但如果它过于复杂,这是一种入门方式。