Improvements and bug with this php to asp.net conversion?
<p>I found <a href="http://tutorialzine.com/2009/09/cool-jquery-gallery/" rel="nofollow">this</a> 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 <a href="http://gallery.xaisoft.com/demo.aspx" rel="nofollow">here</a>.</p>

<p>There are is one issue I am facing and I am also curious if there is anything I could have done differently.</p>

<p>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.</p>

<p>As far as for the code that generates the gallery, I have one method:</p>

<pre><code>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("&lt;div class=\"pic {0}\" 
                                    style=\"background:url('{1}') no-repeat 50% 
                                    50%\"&gt;&lt;a href=\"{2}\" title=\"{3}\" target=\"_blank
                                       \"&gt;{4}&lt;/a&gt;&lt;/div&gt;", 
                                    nomargin, directory + "/" + 
                                    Path.GetFileName(file),
                                    directory + "/" + Path.GetFileName(file), 
                                    Path.GetFileName(file), 
                                    Path.GetFileName(file)));

                i++;

            }

        }

        return sb.ToString();
    }
</code></pre>

<p>I am calling <code>&lt;%=CreateGallery() %&gt;</code> in the demo.aspx.  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 <code>&lt;%=CreateGallery() %&gt;</code> a good option for this or is there a better way.  Other improvements are welcome as well.</p>
