开发者

Implementing javascript in c# code behind

开发者 https://www.devze.com 2022-12-19 00:38 出处:网络
Morning all. I have the following javascript in my code in front <scripttype=\"text/javascript\" src=\"~/VDSReporting/jquery.js\"></script> <script type=\"text/javascript\">

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.getEleme开发者_开发百科ntById('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!


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.


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..

0

精彩评论

暂无评论...
验证码 换一张
取 消