Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Image control won't show ImageURL

Posted on 2012-12-27
4
Medium Priority
?
532 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 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 400 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 800 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

971 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