Solved

Image control won't show ImageURL

Posted on 2012-12-27
4
529 Views
Last Modified: 2012-12-27
I've got a black image control on my webform and a dropdownlistbox which gives a list of jpg files to choose from.  I step threw it and verify the correct path is being sent to Image1.ImageURL but no picture shows up. I can't find any other properties to set on the Image control. Also, why do I have to make pathx static? When it was private (or public) it got set to null when assigning it to target string.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections;

public partial class _Default : System.Web.UI.Page 
{
    static string pathx;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            DirectoryInfo dInfo = new DirectoryInfo(Server.MapPath("pics"));
            pathx = dInfo.FullName;
            ArrayList alFiles = new ArrayList();
           
            alFiles.AddRange(dInfo.GetFiles("*.jpg"));

            foreach (FileInfo fInfo in alFiles)
            {
                DropDownList1.Items.Add(fInfo.Name);
            }
        }
    }
   
    protected void DropDownList1_TextChanged(object sender, EventArgs e)
    {
        string x = DropDownList1.SelectedValue;
        string target = pathx;
        target += "\\";
        target += x;
        Image1.ImageUrl = target;
    }
 
}
 

Open in new window

0
Comment
Question by:zliminator
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 100 total points
ID: 38725890
Also, why do I have to make pathx static?
You don't. The only reason why I can think of that it would be "set to null when assigning it to target string" is that the SelectedValue of the DropDownList was null.

Your pathx variable is set to the full local path of the directory for "pics". This is something anyone not on your physical machine (i.e. everyone not sitting at your computer) would have access to. You need to use the "ASP path". That is "pics" by itself. You use the full local path to retrieve the files from disk; you use the "ASP path" to serve up files to your users.

Try changing line 36 to:

string target = "pics";

Open in new window

0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 200 total points
ID: 38725958
I think this may be a cleaner solution:

protected void DropDownList1_TextChanged(object sender, EventArgs e)
{
    Image1.ImageUrl = ResolveUrl("~/pics/" +DropDownList1.SelectedValue);
}

no need for 'pathx'
0
 

Author Closing Comment

by:zliminator
ID: 38726074
It works but I need to study that article.
0

Featured Post

Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question