<img src="<%=image_path%>new_client/ci-top_01.gif" > ?

utilitaire

Regular
Joined
May 4, 2005
Messages
77
<img src="<%=image_path%>" runat=server> ?????

This is a very simple question: how do I put a string value into a tag runat=server like this:

<img src="<%=image_path%>" runat=server>

This simply doesnt work with the runat=server proprety. IIS does not replace the variable «image_path». I need to do use this variable because my images path is dynamic. Please dont suggest me to set the «src» property of the image control in the code-behind class. This is way too messy since I have more than 300 images in my template ascx. I can't just do:

myImg1.Src = image_path;
myImg2.Src = image_path;
myImg3.Src = image_path;
myImg4.Src = image_path;
myImg5.Src = image_path;
myImg6.Src = image_path;
myImg7.Src = image_path;
... and so on

Any Idea?
 
Last edited:
If I am understanding you correctly then you have a user control that you want a different image to appear based on what page calls the control. You want to set this value in the calling page and let the user control read it.

Try adding a hidden html control and make it public. Set the value to be the value you want for image_path. For example:
Code:
<input id="ImagePath" type="hidden" value="image_path" name="ImagePath" runat="server"><br>
Your code behind declaration of the hidden control should read:
Code:
Public WithEvents ImagePath As System.Web.UI.HtmlControls.HtmlInputHidden

When you incorporate the usercontrol into a page set the value of the control to be the image path you want. For example:
Code:
MyUserControl.ImagePath.Value = "theactualimagepath"

Then again, back at your usercontrol. In your page load set the src of your image control from the hidden control.
Code:
myImg1.src = ImagePath.Value

I know that is a bit of a complicated explanation but I did my best to make it clear!

HTH
Eva
 
evaleah said:
If I am understanding you correctly then you have a user control that you want a different image to appear based on what page calls the control. You want to set this value in the calling page and let the user control read it.

Try adding a hidden html control and make it public. Set the value to be the value you want for image_path. For example:
Code:
<input id="ImagePath" type="hidden" value="image_path" name="ImagePath" runat="server"><br>
Your code behind declaration of the hidden control should read:
Code:
Public WithEvents ImagePath As System.Web.UI.HtmlControls.HtmlInputHidden

When you incorporate the usercontrol into a page set the value of the control to be the image path you want. For example:
Code:
MyUserControl.ImagePath.Value = "theactualimagepath"

Then again, back at your usercontrol. In your page load set the src of your image control from the hidden control.
Code:
myImg1.src = ImagePath.Value

I know that is a bit of a complicated explanation but I did my best to make it clear!

HTH
Eva

Thank you for you answer. But this line is what I want to avoid:

"myImg1.src = ImagePath.Value"

Like I said, I have hundreds of image Control in my pages. I absolutely cannot initialyse all these controls in the code-behind, because it would be a mess (and a total horror) for the programmers. I'd be forced to declare all thoses control and initialyse them in the code behind. Awfull. I just want to add controls in the html code without having to deal with them in the code-behind.

Thank you again. If you have another solution please help me!
 
I guess I am confused about what you are really trying to do. You have to set the value of the variable somewhere. Where are you setting the value of the variable? Aren't you doing that in code?

If you aren't doing it in code somewhere it's not dynamic. In that case just put in your image path string.
 
evaleah said:
I guess I am confused about what you are really trying to do. You have to set the value of the variable somewhere. Where are you setting the value of the variable? Aren't you doing that in code?

If you aren't doing it in code somewhere it's not dynamic. In that case just put in your image path string.

Here's what I do in the code behind:

Code:
if(myculture == ENGLISH)
image_path = "http://mysite.com/images/en/";
else 
image_path = "http://mysite.com/images/fr/";

Here's what I do in my aspx page:

Code:
<img src="<%=image_path%>" id=img1 runat=server>
<img src="<%=image_path%>" id=img2 runat=server>
<img src="<%=image_path%>" id=img3 runat=server>
<img src="<%=image_path%>" id=img4 runat=server>

As you can see, I set a value to "image_path". This value depens actually on the cultureinfo. All the images are both in english and in french. There's a separated directory for both languages. This is why I said the path was «dynamic». I dont know if you ever faced this kind of problem with runat=server controls. When I work with html tag, not control, it works fine. But if I put the «runat=server» into the tag, the string wont be replaced be the value that I setup in the code-behind. :confused: :confused: :confused:
 
As I said, I want my code-behind to be very clean(I dont know if its the correct word). I accept to do this part in the code-behind, as it is very short and I can encapsulate this so the programmers wont have to deal with it in the future:

Code:
if(myculture == ENGLISH)
image_path = "http://mysite.com/images/en/";
else 
image_path = "http://mysite.com/images/fr/";

But here's what I dont want to do:

Code:
if(myculture == ENGLISH)
image_path = "http://mysite.com/images/en/";
else 
image_path = "http://mysite.com/images/fr/";

img1.src = image_path + "img1.gif";
img2.src = image_path + "img2.gif";
img3.src = image_path + "img3.gif";
img4.src = image_path + "img4.gif";
img5.src = image_path + "img5.gif";
img6.src = image_path + "img6.gif";
img7.src = image_path + "img7.gif";
img8.src = image_path + "img8.gif";
img9.src = image_path + "img9.gif";

You agree that is way too long. I absolutely cant do that.
 
And why don't you do a loop to assign all the values?

you can loop from 1 to NUMBER_OF_IMAGES and try to find the control imgX in every round. When you have the control:
imgX.src = image_path + "img" + i.ToString + ".gif";

Have you tried?
 
Jackpanel said:
<%="<img src='" & image_path & "img1.gif'>" %>

Right, I know that part. What I don't know is FROM where you want to SET that value of "image_path". Just putting a variable someplace doesn't assign it a value. How did you *want* to assign the value? Where were you thinking of doing that?

Maybe if you tell me that there might be other options. It's just that you have to assign a value somewhere in order to read it here.

Does what I am asking you make more sense now?
 
evaleah said:
Right, I know that part. What I don't know is FROM where you want to SET that value of "image_path". Just putting a variable someplace doesn't assign it a value. How did you *want* to assign the value? Where were you thinking of doing that?

Maybe if you tell me that there might be other options. It's just that you have to assign a value somewhere in order to read it here.

Does what I am asking you make more sense now?

Here's what I do:

Code:
public class MyPage: Page{

protected string image_path;

public void page_load(...){
image_path = "http://mysite.com/fr/";
}

}

As you can see, the image_path string can be access throught the webform that inherits from «MyPage».
 
Jackpanel said:
<%="<img src='" & image_path & "img1.gif'>" %>

Right, but what do I do if I want to make this image tag a Control?

<%="<img src='" & image_path & "img1.gif' runat=server>" %>

This wont work indeed. It will result in a string sent to the browser, but it will never be executed as a control.
 
Hyuga said:
And why don't you do a loop to assign all the values?

you can loop from 1 to NUMBER_OF_IMAGES and try to find the control imgX in every round. When you have the control:
imgX.src = image_path + "img" + i.ToString + ".gif";

Have you tried?

Yeah it would work, in theory. But it would still become a mess:

1) I have hundred of images in my site. There is no way the designers will name all the images «imgX.gif». Too messy. If all images have a different name, how can I loop?

2) With hundred of images, it would slow down the web site. I'd have to search recursivly from the root control throught all the controls in the page in order to find all the images.

3) The same problem will appear with background properties in table, td, tr, body. The same problem will appear in «input type=submit» button, «input type=button» button, etc.

I think you'll agree that its way too complicated and way too heavy.
 
utilitaire said:
But here's what I dont want to do:

Code:
if(myculture == ENGLISH)
image_path = "http://mysite.com/images/en/";
else 
image_path = "http://mysite.com/images/fr/";

img1.src = image_path + "img1.gif";
img2.src = image_path + "img2.gif";
img3.src = image_path + "img3.gif";
img4.src = image_path + "img4.gif";
img5.src = image_path + "img5.gif";
img6.src = image_path + "img6.gif";
img7.src = image_path + "img7.gif";
img8.src = image_path + "img8.gif";
img9.src = image_path + "img9.gif";

You agree that is way too long. I absolutely cant do that.

I have to admit, I don't understand why you don't want to do that. You won't take a performance hit to do it in the code rather than in the html. It's still faster to do it this way than to create the controls at runtime.

Since you need the path to be dynamic and all the images are different, I'm afraid this is the most effecient way. Especially if all the image names are not something you can loop.

Otherwise, the loop option is the best you've got. If you name all your img html objects the same as the images you are looking for you could run your loop that way.

It just seems to me you are avoiding one method without really gaining anything. Whether you set the variable "image_path" or the src itself in code, either way you have to set something in code. The easiest and fastest way to get the image you want is to set it as we have all suggested here.
 
I'm not so sure...


1) My web site has more than 2000 images, with specific names like «button_leave.gif». You're telling me to rename all those images in the «imgX.gif» form??? :o

2) Are you so sure that I dont take a performance hit while doing a recursive loop? Everytime a page is called(this has to be done in every Request), IIS would have to loop recursively throught ALL the controls in my page. For each control, loop throught all controls, in witch you loop recursively throught all controls, in witch you loop recursively in all controls... I dont know you, but to me it seems a bit heavy. My web site has more than 4 million request per day. I absolutely cannot sacrifice performance here. Are you sure it is faster than simply doing a response.write in the webform?

3) If it was only for images, it would be easier to program. But this philosophy would have to be apply to all sort of control that take a «src» attribute. If I want to use a «asp:button» control, I will have to do the same thing(recursive loop) in the code behind. Very depressive dont you think??? :confused:

4) Since I like to keep the code-behind very clean, it would be very sad to put 200 control declaration in the class definition. Imagine how horrible it would look like. Asp.net is supposed to be faster than classic asp. I dont want the programmers to comit suicide everytime they have to put some image control in their webform! ;)
 
I cant believe I'm the only one in the world who faced this problem when using controls with dynamic image path. How do you deal with it??? If you have a magic solution, please tell me! How do you manage to inform the browser that all images are either in the "fr" directory, or in the "en" directory, or in the "sp" directory, depending on the cultureinfo the client is using.

www.mysite.com/fr/demo.gif
www.mysite.com/en/demo.gif
www.mysite.com/sp/demo.gif
...

How would you do it? I'm looking for a solution that is:

1) performant (large amount of visitors)
2) clean (I mean that I dont want to write 3 hundred lines of code just to initialyse the images src.)

thank you for your help!
 
Probably your best solution would be to loop through all image controls and adjust the ImageURL property to the right folder.

Here's an example, where I'm setting the folder to pick from using the sLanguage variable. I go through all controls in the page, and if its an image, replace the "~" in the image path with a "~/FR". This requires your images all have the url set to "~/imagename.gif" style, but you can adjust it if you have a different setup:

Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sLanguage As String = "FR"
        SetImagePath(Page, sLanguage)
    End Sub

    Private Sub SetImagePath(ByVal p_Control As Control, ByVal p_Language As String)
        Dim ctrl As Control
        For Each ctrl In p_Control.Controls
            If TypeOf ctrl Is System.Web.UI.WebControls.Image Then
                Dim sNewPath As String
                sNewPath = CType(ctrl, System.Web.UI.WebControls.Image).ImageUrl.Replace("~", "~/" & p_Language)
                CType(ctrl, System.Web.UI.WebControls.Image).ImageUrl = sNewPath
            Else
                If ctrl.Controls.Count > 0 Then
                    SetImagePath(ctrl, p_Language)
                End If
            End If
        Next
    End Sub
 
Jackpanel said:
Probably your best solution would be to loop through all image controls and adjust the ImageURL property to the right folder.

Here's an example, where I'm setting the folder to pick from using the sLanguage variable. I go through all controls in the page, and if its an image, replace the "~" in the image path with a "~/FR". This requires your images all have the url set to "~/imagename.gif" style, but you can adjust it if you have a different setup:

Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sLanguage As String = "FR"
        SetImagePath(Page, sLanguage)
    End Sub

    Private Sub SetImagePath(ByVal p_Control As Control, ByVal p_Language As String)
        Dim ctrl As Control
        For Each ctrl In p_Control.Controls
            If TypeOf ctrl Is System.Web.UI.WebControls.Image Then
                Dim sNewPath As String
                sNewPath = CType(ctrl, System.Web.UI.WebControls.Image).ImageUrl.Replace("~", "~/" & p_Language)
                CType(ctrl, System.Web.UI.WebControls.Image).ImageUrl = sNewPath
            Else
                If ctrl.Controls.Count > 0 Then
                    SetImagePath(ctrl, p_Language)
                End If
            End If
        Next
    End Sub


I thought about it and its not so bad. The major problems are:

1) You have to do a recursive loop throught all controls in all sub-usercontrols. Isn't it heavy???

2) I would need to do this not only for System.Web.UI.WebControls.Image. But also for:

-ImageButton
-Input type="image"
-Image
-Body
-Table backgroud="http://..."

Its a lot of exceptions dont you think? Every time I discover another control who need a source image, I'd have to update my pages?

3) What if I want some images not to refer to my image_path? Suppose most of images refer to "image_path", but 10 images in the whole site refer to another domain name(http://www.google.com/images/toto.gif" for instance). The loop would convert the google link into: image_path + "http://www.google.ca/images/toto.gif" witch is wrong. :confused:
 
Back
Top