简体   繁体   中英

C# asp.net calling javascript

I have a div inside asp:content :

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div id="slidebar" style="display:none;"  >There are some pending approvals.Please    approve    by the 25th of this month

<a href="#" id="slink" style="margin-left:10px;" onclick="fade('slidebar');">Click here to   close <sup style="font:caption;color:#373636;">X</sup></a>
</div>

<script language="javascript" type="text/javascript">
 function showbanner()
  {
   document.getElementById("slidebar").style.visibility="visible";
  }
</script>
<\asp:content>

and the code behind:

 ClientScript.RegisterClientScriptBlock(Page.GetType(), "displaybanner", "return  showbanner();", true);

I cannot call the function showbanner from the code behind and even when I directly call the statement inside showbanner with registerclientscript ...it doesnt get called .

Please help

The properties visibility and display are not the same, change the js function to:

function showbanner()
{
    document.getElementById("slidebar").style.display="block";
}

also change your code behind to

ClientScript.RegisterStartupScript(Page.GetType(), "displaybanner", "showbanner();", true);

so the script executes after the page has load or else it won't find the element.

I'm not sure why your register script isn't working but have you tried putting the javascript call directly on the page inside a script block? If that works then wrap the script block in a pannel at the bottom of the page with visible="false". In your code behind when you determine that you want it to work set the visibility to true.

If you are using Jquery I would also wrap the contents of your script block in:

$(document).ready(function() {
    //javascript call here
});

Just to make sure it doesn't get called before the page can actually handle it.

This all assumes of course that your function call is good and that the issue is with register client script.

Okay one of my team mebers Mahi came up with a solution...rather than CleientScript.reg.....

we used Page.RegisterStartupScript and it works fine :))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM