Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing an Image's Source with ASP.NET 1

Status
Not open for further replies.

nevets2001uk

IS-IT--Management
Jun 26, 2002
609
GB
I'm trying to figure out the best way of allowing variable images on my web page and wondered if anyone might have an idea.

What I want to do is have a set of images for the page that will change depending on the colour scheme the user selects. This could be a large number of images on each page.

The way I'm thinking of doing it is to have seperate image directories for each scheme and to have a variable for the path to each of those folders which changes depending on the scheme in use.

However I'm not really sure how I could change the source values in the image tags on the html page? Ideally something like <img src=variableforpath & &quot;imagename.gif&quot; /> or something would work but how can I get that variable into the code?

Cheers!

Steve Gordon
 
You could use:
Code:
In your .aspx page
<asp:Image ID=&quot;img1&quot; Runat=&quot;server&quot;></asp:Image>

In your code behind
select case scheme_used
 case &quot;scheme1&quot;
  img1.ImageUrl=&quot;scheme1.jpg&quot;
 case &quot;scheme2&quot;
  img1.ImageUrl=&quot;scheme2.jpg&quot;
 case else
  img1.ImageUrl=&quot;default_scheme.jpg&quot;
end select
 
Thanks. I had considered something like that but my only problem with it was that since I will have a large number of images on the page (including repeated ones, as they are for the layout elements) I was hoping to not have to create a asp image control for each one.

I was hoping for something more like a single variable that has to change with the rest being pretty much standard HTML. If your advice is the only real way to do it though I guess I'll have to go that way.

Cheers

Steve Gordon
 
That I was just trying to tell you! You will have your image controls just where you want them! You will put them on your page in the positions you want them. Thru server side script and depending on the schema chosen, you will simply change their ImageUrls ... you don't need to recreate them!
Make a Sub that is called in your Page_Load like this:
Code:
sub Page_Load
...
make_layout()
...
end sub

sub make_layout()
 select case scheme_chosen (this could be a session.item in e.g.)
  case &quot;scheme1&quot;
   img_top.ImageUrl=&quot;scheme1_top.jpg&quot;
   img_bottom.ImageUrl=&quot;scheme1_bottom.jpg&quot;
   img_left.ImageUrl=&quot;scheme1_left.jpg&quot;
   ...
  case &quot;scheme2&quot;
   img_top.ImageUrl=&quot;scheme2_top.jpg&quot;
   img_bottom.ImageUrl=&quot;scheme2_bottom.jpg&quot;
   img_left.ImageUrl=&quot;scheme2_left.jpg&quot;
   ...
  case else
   img_top.ImageUrl=&quot;scheme_default_top.jpg&quot;
   img_bottom.ImageUrl=&quot;scheme_default_bottom.jpg&quot;
   img_left.ImageUrl=&quot;scheme_default_left.jpg&quot;
   ...
 end select
end sub
Code:
img_top, img_bottom, img_left, ... etc
are created only once on your aspx page and placed on the positions you need them.
I hope you got the idea ...
 
I think what he wanted was to use something like this:

html
Code:
<link href=&quot;<%#schemePath%>scheme.css&quot; type=&quot;text/css&quot; rel=&quot;stylesheet&quot;>

<img src=&quot;<%#schemePath%>logo.gif&quot;>
<img src=&quot;<%#schemePath%>button1.gif&quot;>

etc...

code
Code:
protected string schemePath;

Page_Load(Object sender, EventArgs e){

    if(!Page.IsPostBack){

        switch(Context[&quot;current_scheme&quot;].ToString()){
            case &quot;blue&quot;:
                schemePath= &quot;/schemes/blue/&quot;;
                break;
            case &quot;red&quot;:
                schemePath = &quot;/schemes/red/&quot;;
                break;
            default:
                schemePath = &quot;/schemes/default/&quot;;
                break;
        }

        this.DataBind();
     }

}

HTH



David
[pipe]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top