繁体   English   中英

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

[英]How To Display image dynamically in 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=??

如何进行同样的操作直到文件末尾?

一种方法是将图像和链接放置在更新面板中,并由链接触发更新面板。 在服务器上,单击链接的onclick并仅更新图像源和链接。

另一种方法是使用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;),然后从会话中获取数组(例如,oFileList = Session [“ imageFiles”];)

这不是最佳性能的方法,但是很简单。

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

Dima的答案要好得多,但是如果答案太复杂,这是入门的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM