Click to See Complete Forum and Search --> : Changing an image dynamically with C#


r1pt1de
11-20-2006, 12:14 PM
Hey guys I hope this is where this is supposed to be :P.

I am building a site for work, and all the code so far has been written in C# in the code behind pages. I am slightly new to this but learning a lot quickly.
Ok here is the scenario. I have a main start page that the user will make a few selections on. That page sends 2 variables to a new page based on the selections.

string type="";
string imageurl = "";


if (H_Duct.Checked)
{
if (H_Coil.SelectedValue == "1")
type = "H3D1C";
else if (H_Coil.SelectedValue == "2")
type = "H3D2C";
else if (H_Coil.SelectedValue == "3")
type = "H3D3C";
}
imageurl = "~/images/" + type + ".gif";
testing.NavigateUrl = "main.aspx?type=" + type + "&imageurl=" + imageurl;

Thats the code that is passing the values. This is working just fine, and im able to change all the form fields dynamically based on that. I also need to have the page determine which image to display based on those selections i know its passing the URL, and I know the page is getting it (debug with break mode is nice). But for some reason I can not get it to display the image. here is the code I cuttently have...

protected void Page_Load(object sender, EventArgs e)
{
string type = Request.QueryString["type"].ToString();
if (duct_image.ImageUrl == "")
{
string imageurl = Request.QueryString["imageurl"].ToString();
duct_image.ImageUrl = imageurl;

}

Now, I know its passing it right, and I know that the line to set the duct_image URL is working due to stopping it in break mode. Just cant get it to display =(.
If anyone could help me out with this i would be very greatful. Thanks in advance

sirpelidor
11-21-2006, 10:58 AM
firefox won't allow u to use absolute path for security reason (which is good). IE allows absolute path because it built-in with window explorer. You can use Server.MapPath (http://www.4guysfromrolla.com/webtech/121799-1.shtml) instead of building a entire path to your query string.

here's what I would do if i were you...


string type="";
if (H_Duct.Checked)
{
switch(H_Coil.SelectedValue){
case "1": type = "H3D1C.gif"; break;
case "2": type = "H3D2C.gif"; break;
case "3": type = "H3D3C.gif"; break;
default: throw new Exception("User Click nothing!");
}
}
Response.Redirect(@"main.aspx?type=" + type;


on ur new page, this is where we use Server.Mappath...


protected void Page_Load(object sender, EventArgs e)
{
if(!this.IsPostBack){
string type = Request.QueryString["type"].ToString();
duct_image.ImageUrl = Server.MapPath("/images/" + type);
}//end not post back

vladibo
11-21-2006, 06:22 PM
Don't use duct_image.ImageUrl = Server.MapPath("/images/" + type); !!!

This should give you the machine path not web URL.

I guess that probably if (duct_image.ImageUrl == "") is never true

sirpelidor
11-21-2006, 07:43 PM
Don't use duct_image.ImageUrl = Server.MapPath("/images/" + type); !!!

This should give you the machine path not web URL.

I guess that probably if (duct_image.ImageUrl == "") is never true


oh sorry about that. I wasn't thinking as I was responding this thread. Yes, -do not- use Server.MapPath because you are not saving files to the server.

take a look at your aspx form, and make sure duct_image didn't already contain an image path, hence you won't hit if(duct_image.ImageUrl == "") at all.

thanks vladibo :)

r1pt1de
11-22-2006, 10:39 AM
Actually i specifically set the imageURL to a blank value that way (duct_image.ImageUrl == "") was true on the initial load of the page. Firefox gives me an error everytime I try to do a response.redirect saying that the redirect would never complete or something like that ( im assuming this is because of the == "" statement), and when i attempt to open it in IE it just continues looping trying to load the page, and never loads anything. And the images are stored on the central server, so would MapPath work? I dont really care if they are able to see the machine path because the only people viewing this are in my building, and I have a log of everything that hits each page. So security really isnt a big issue.

And thank you sirpelidor the first part of code actually solved another problem LOL.

Even MapPath wont cause it to display the image. I know its getting the path to it, i can see the link displayed properly. Just not actually showing the picture itself for some reason.
________________________________________________

Ok we lost internet for a bit so I never got to post the reply. Soooo heres part 2 mixed in with part 1 :P. Ok started looking at a few things based on your suggestions. I can use Server.MapPath because the images are getting stored on the site. I just have to put the ~ in front of the leading /. Still leaving me in the same spot. Its posting the path, but not displaying the picture. The browser even says its there. But its not showing it for some reason. If i right click and view selections source, it shows me the correct path, and even the "alt" text is not showing. So I know im close, just cant get it to actually display.

So i start taking a look at it,. I right click the image and copy image path. place it in the address bar, and gee wiz look at that, resource cant be found. I had misnamed all the images :P lol I got it working. Thanks you two. Really do appreciate it.