Solved

Image control won't show ImageURL

Posted on 2012-12-27
4
519 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
  • 2
4 Comments
 
LVL 74

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 Comment

by:zliminator
ID: 38726037
0
 

Author Closing Comment

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to dynamically set the form action using jQuery.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now