Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Image control won't show ImageURL

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

Simplify Your Workload with One Tool

How do you combat today’s intelligent hacker while managing multiple domains and platforms? By simplifying your workload with one tool. With Lunarpages hosting through Plesk Onyx, you can:

Automate SSL generation and installation with two clicks
Experience total server control

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
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.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

704 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