简体   繁体   English

C#asp.net调用javascript

[英]C# asp.net calling javascript

I have a div inside asp:content : 我在asp里面有一个div:内容:

<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 . 我无法从后面的代码中调用函数showbanner,甚至当我直接使用registerclientscript调用showbanner中的语句时......它也不会被调用。

Please help 请帮忙

The properties visibility and display are not the same, change the js function to: 属性visibilitydisplay不一样,将js函数更改为:

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? 我不确定为什么你的注册脚本不能正常工作,但你是否尝试将javascript调用直接放在脚本块内的页面上? If that works then wrap the script block in a pannel at the bottom of the page with visible="false". 如果可行,则将脚本块包装在页面底部的pannel中,其中visible =“false”。 In your code behind when you determine that you want it to work set the visibility to true. 在您确定要使用它的代码后面,将可见性设置为true。

If you are using Jquery I would also wrap the contents of your script block in: 如果您正在使用Jquery,我还会将脚本块的内容包装在:

$(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..... 好吧,我的团队成员Mahi想出了一个解决方案...而不是CleientScript.reg .....

we used Page.RegisterStartupScript and it works fine :)) 我们使用Page.RegisterStartupScript,它工作正常:))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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