简体   繁体   English

如何在asp.net的图像查看器中查看列表框中的选定项目

[英]How to view selected item in listbox in image viewer asp.net

I have this code that populates my listbox that contains what I type in the textbox. 我有这段代码可填充列表框,其中包含我在文本框中键入的内容。 My problem is how can I view the selected item in my listbox in a image viewer since all my files are images? 我的问题是,由于我所有的文件都是图像,因此如何在image viewer listbox查看所选项目? Am I missing something? 我想念什么吗?

Here's my code: 这是我的代码:

protected void Button1_Click(object sender, EventArgs e)
    {
        ListBox1.Items.Clear();
        string[] files = Directory.GetFiles(Server.MapPath("~/images"), "*.*", SearchOption.AllDirectories);


        foreach (string item in files)
        {
            string fileName = Path.GetFileName(item);
            if (fileName.ToLower().Contains(TextBox1.Text.ToLower()))
            {
                ListBox1.Items.Add(fileName);
            }

        }
    }

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DocumentImage.ImageUrl = Directory.GetDirectories("~/images") + ListBox1.SelectedItem.ToString();
        }

This should work I think: 我认为这应该工作:

protected void Button1_Click(object sender, EventArgs e)
{
    ListBox1.Items.Clear();
    string[] files = Directory.GetFiles(Server.MapPath("~/images"), "*.*", SearchOption.AllDirectories);
    foreach (string item in files)
    {
        string fileName = Path.GetFileName(item);
        if (fileName.ToLower().Contains(TextBox1.Text.ToLower()))
        {
            string subPath = item.Substring(Server.MapPath("~/images").Length).Replace("\\","/");
            ListBox1.Items.Add(new ListItem(fileName, subPath));
        }
    }
}

In this part, you need to not only have the file name, but also the path where the file was found. 在这一部分中,您不仅需要具有文件名,还需要找到文件的路径。 In my sample, the sub path where the file was found is first set into subPath and that is then stored as the value for the list item. 在我的示例中,首先将找到文件的子路径设置为subPath ,然后将其存储为列表项的值。

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    DocumentImage.ImageUrl = "~/images" + ListBox1.SelectedItem.Value;
}

Here we use the sub path to set the correct url to the image. 在这里,我们使用子路径为图像设置正确的url。

Note that you need to have the AutoPostBack set to true on the DocumentImage in your asxp page in order for the image to change when you change the selection in the list box. 请注意,您需要在asxp页面上的DocumentImage asxp AutoPostBack设置为true,以便在更改列表框中的选择时更改图像。

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

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