tags:

views:

315

answers:

2

Morning all.

I have the following javascript in my code in front

     <script  type="text/javascript" src="~/VDSReporting/jquery.js"></script> <script type="text/javascript">

    function ShowImage() {
        document.getElementById('tbxProdAC') 
      .style.backgroundImage = 'url(/images/vds/progress.gif)';

        document.getElementById('tbxProdAC')
                    .style.backgroundRepeat = 'no-repeat';

        document.getElementById('tbxProdAC')
                    .style.backgroundPosition = 'right';
    }

    function HideImage() {
        document.getElementById('tbxProdAC')
                      .style.backgroundImage = 'none';
    } 

</script>

How do I go about 'converting' this and only having it present in c# code behind?

Please excuse my ignorance, I'm completely out of my depth here!

+4  A: 

If this is a progress image you are showing (seems so from the image name), then why would you want to do that server side? That will kind of defeat the whole purpose of a progress image. This seem like it belong on the client side, so keep it there.

Update

You don't need to use the code behind to render the script just to get the client id's. You can do something like this:

function ShowImage() {
    document.getElementById('<%=tbxProdAC.ClientID%>') 
  .style.backgroundImage = 'url(/images/vds/progress.gif)';

    document.getElementById('<%=tbxProdAC.ClientID%>')
                .style.backgroundRepeat = 'no-repeat';

    document.getElementById('<%=tbxProdAC.ClientID%>')
                .style.backgroundPosition = 'right';
}

function HideImage() {
    document.getElementById('<%=tbxProdAC.ClientID%>')
                  .style.backgroundImage = 'none';
} 

Here I use <%=tbxProdAC.ClientID%> to get the id of the control. This is a lot more readable then using the code behind to render the script.

Mattias Jakobsson
Hello Mattias - see above for my little issue and my thinking behind it. I'm completely new to all of this so trying to find work arounds for problems I am encountering.Essentially this javascript is failing now that it is place within a content place holder.
Ricardo Deano
See the updated answer.
Mattias Jakobsson
Brilliant - thank you Mattias, your help is greatly appreciated.
Ricardo Deano
No problems. Glad I could help.
Mattias Jakobsson
A: 

I need some javascript to be run from the server side and this worked for me -

if (!this.ClientScript.IsStartupScriptRegistered("StartDown"))
{
     string scriptString = @"alert(""some javascript"");location.href='MyPage.aspx';";
     this.ClientScript.RegisterStartupScript(Page.GetType(), "StartDown", scriptString, true);
} 

Hope this helps..

Pavanred