0

I'm trying to change ASP image control's image source by javascript but change does not effect immediately.

ASP image control in ASCX

<asp:Image  ImageAlign="Middle" runat="server" ID="ProcurementImageIconControl" ImageUrl="~/Images/Icon_Blue.gif?34" Style="cursor: pointer;" visible="false" />    

Javascript that is included in ASCX that triggers image change.

        if (isProcurementLineItemExists != "0") {
            Image.src = Image.src.replace("Blue","Green");
        }
        else {
            Image.src = Image.src.replace("Green", "Blue");
        }

Above is my testing code in included javascript file. Image changes does not take effect immediately. Image show correct image on the next load so it leads me to believe that function is getting called correctly it just doesn't update image right away.

I have tried adding "?sdf=" with random numbers and what not, but no matter what I do image does not refresh.

Can anyone help me with this problem?

Jon P
  • 19,442
  • 8
  • 49
  • 72
Anatoli
  • 922
  • 2
  • 11
  • 25
  • Can you post some more code so we can see how and when this code is being called? – James Johnson Sep 07 '11 at 15:47
  • I'm extending existing code that is gigantic. If you could specify it would be more manageable for me to show it to you. – Anatoli Sep 07 '11 at 16:27
  • 1
    What is triggering the image change? Is it a button click or onload, or elsewhere? I'm wondering if you might be changing the image too late. Also, did you try setting `EnableViewState` to false on the Image? – James Johnson Sep 07 '11 at 16:40
  • Above javascript will fire when a user clicks a button to bring up an ascx page. And i'm leaning towards the idea that i'm changing image is too late. But considering this is existing and massive code, either I tinker this to work or rewrite, which can't be an option... – Anatoli Sep 07 '11 at 16:46
  • Is it possible to post the button click code at least? The current code is not enough to answer the question with any amount of confidence. – James Johnson Sep 07 '11 at 16:50
  • That is kinda tricky... Because aspx page calls a javascript object's function and that calls ascx page's javascript and changes img source. And between those, there are a lot of codes. – Anatoli Sep 07 '11 at 16:56

1 Answers1

0

This is not a duplicate question, but if I were you I would really use JQuery and follow examples already present here on SO, for example:

Change the image source on rollover using jQuery

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147