Solved

Image control won't show ImageURL

Posted on 2012-12-27
4
526 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how can i make amazon approved mobile url 4 37
Problem to Office 1 42
TFS 2015 Access denied. 1 30
Political Map of Europe - Wikipedia? 4 22
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

756 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