简体   繁体   English

改进和此PHP到ASP.NET转换的错误?

[英]Improvements and bug with this php to asp.net conversion?

I found this tutorial on how to take a directory of images and build a gallery out of it and apply the light-box plug in to it. 我发现对如何拍摄图像的目录,并建立一个画廊了出来,并应用光盒插入到它的教程。 I do not know php, but I tried to take the code and do an asp.net/c# conversion of the tutorial and I got it working here . 我不了解php,但我尝试采用该代码并对本教程进行asp.net/c#转换,然后在这里运行

There are is one issue I am facing and I am also curious if there is anything I could have done differently. 我面临着一个问题,我也想知道是否可以做一些不同的事情。

The issue I am having is that when you click on an image that is not the first or last image and you use the arrow keys to go forward and back, it is going from image 12 for example to 10 to 8 to 6 and so on or it might go from 2 to 5 to 7. I can't figure out why this is happening. 我遇到的问题是,当您单击不是第一张或最后一张图像的图像时,使用箭头键向前和向后前进时,它从图像12变为例如10到8到6等。否则可能会从2变为5到7。我不知道为什么会这样。

As far as for the code that generates the gallery, I have one method: 至于生成图库的代码,我有一种方法:

public string CreateGallery()
{
    StringBuilder sb = new StringBuilder();

    string directory = "gallery";
    string[] allowedTypes = new string[] { "jpg", "jpeg", "gif", "png" };

    string[] file_parts = new string[] { };


    string ext = "";
    string title = "";
    int i = 0;

    string[] files = System.IO.Directory.GetFiles(Server.MapPath(directory));


    foreach (var file in files)
    {

        file_parts = Path.GetFileName(file).Split('.');
        ext = file_parts[1];
        title = file_parts[0];

        string nomargin = "";

        if (allowedTypes.Contains(ext))
        {
            if ((i + 1) % 4 == 0) nomargin = "nomargin";

            sb.Append(string.Format("<div class=\"pic {0}\" 
                                    style=\"background:url('{1}') no-repeat 50% 
                                    50%\"><a href=\"{2}\" title=\"{3}\" target=\"_blank
                                       \">{4}</a></div>", 
                                    nomargin, directory + "/" + 
                                    Path.GetFileName(file),
                                    directory + "/" + Path.GetFileName(file), 
                                    Path.GetFileName(file), 
                                    Path.GetFileName(file)));

                i++;

            }

        }

        return sb.ToString();
    }

I am calling <%=CreateGallery() %> in the demo.aspx. 我在<%=CreateGallery() %>调用<%=CreateGallery() %> I basically copied the code almost verbatim, so is there anything that I am doing that I do not have to do (unecessary). 基本上,我几乎逐字复制了代码,所以有什么我不需要做的事情(不必要的)。 Is calling <%=CreateGallery() %> a good option for this or is there a better way. <%=CreateGallery() %>调用<%=CreateGallery() %>是一个不错的选择,或者有更好的方法。 Other improvements are welcome as well. 也欢迎其他改进。

I've figured out the problem in more specific terms, but I don't really have a solution for you. 我已经用更具体的术语解决了这个问题,但是我真的没有适合您的解决方案。

Clicking on any image works at first. 首先单击任何图像即可。 The problem begins when you close an image and choose a new one. 当您关闭图像并选择一个新图像时,问题就开始了。 For every image you've clicked on, the arrow keys will move by 1 picture. 对于您单击的每个图像,箭头键将移动1张图片。 So on your first image choice, it will work perfectly; 因此,在您选择第一个图像时,它会完美运行; each arrow key hit will move by one picture. 每按一次箭头键将移动一张图片。 When you've closed the first image and clicked a second, the arrow keys will move by 2 pictures. 关闭第一张图像并单击第二张图像后,箭头键将移动2张图片。 Three images makes the arrow keys move by three pictures. 三幅图像使箭头键移动三幅图像。

My guess is that you're creating some kind of event handler for the arrow keys whenever a user clicks on an image, and not removing that event handler when the image is closed. 我的猜测是,每当用户单击图像时,您都将为箭头键创建某种事件处理程序,而在图像关闭时不会删除该事件处理程序。 So after clicking three images, you've got three event handlers moving along the picture list on an arrow key click. 因此,单击三个图像之后,单击箭头时,就会有三个事件处理程序沿图像列表移动。

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

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