C# asp.net calling javascript

瘦欲@ 提交于 2019-12-23 13:06:28

问题


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


回答1:


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.




回答2:


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.




回答3:


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

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



来源:https://stackoverflow.com/questions/5185521/c-sharp-asp-net-calling-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!