繁体   English   中英

C#asp.net调用javascript

[英]C# asp.net calling javascript

我在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>

和背后的代码:

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

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

请帮忙

属性visibilitydisplay不一样,将js函数更改为:

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

也将你的代码改为

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

所以脚本在页面加载后执行,否则它将找不到元素。

我不确定为什么你的注册脚本不能正常工作,但你是否尝试将javascript调用直接放在脚本块内的页面上? 如果可行,则将脚本块包装在页面底部的pannel中,其中visible =“false”。 在您确定要使用它的代码后面,将可见性设置为true。

如果您正在使用Jquery,我还会将脚本块的内容包装在:

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

只是为了确保在页面实际处理它之前不会调用它。

这一切都假定您的函数调用是好的,并且问题在于寄存器客户端脚本。

好吧,我的团队成员Mahi想出了一个解决方案...而不是CleientScript.reg .....

我们使用Page.RegisterStartupScript,它工作正常:))

暂无
暂无

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

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