页面中加入了UpdatePanel后,Response.Write("<script>function dis (){alert('这是调用写在server的JS');}</script>")来调用客户端脚本,无任何提示的无反应。难道没有办法调用客户端脚本了?
     方法是有的,那就是采用 ScriptManager.RegisterStartupScript(Control controlId,Type this.GetType(),String key,String script block)方法。ps:有见到过帖子上说controlId必须是UpdatePanel里的,其实不然,page控件就可以。
     下面给出一个具体的实例:
  
 sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(BtnJs, this.GetType(), "alert""<script>function

       dis (){alert(
'这是调用写在server的JS,如用Response.Write()是不能实现此效果的!!!      ');}</script>", false);
     
 
      
         UpdatePanel中调用JS代码,如alertUpdatePanel中调用JS代码,如alert
        </asp:ScriptManager>
        
<input id="BtnJs" type="button" value="CallServerJs" onclick="dis()" runat="server"/>
        
<asp:UpdatePanel ID="UpdatePanel1" runat="server"  UpdateMode="Conditional"  RenderMode="Block">
            
<ContentTemplate>
               
<asp:Button ID="Button6" runat="server" Text="Button" OnClientClick="dis()"/>
            
</ContentTemplate>     
        
</asp:UpdatePanel> 


注意BtnJs是UpdatePanel外的按钮  同时Button6重用了服务端注册的脚本

    附带说一下,如果是在普通的aspx中希望在服务器端注册下客户端脚本,可以用
Page.ClientScript.RegisterStartupScript(this.GetType(), String Key,String Js block ,Bool   AddScriptTag),除了注意粗体字以外,其他操作同上。紫色部分不能包含,如写了的话,浏览时会将脚本部分自动注释掉!
        

  



相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-03-07
  • 2022-12-23
  • 2021-07-25
  • 2021-11-30
  • 2021-12-15
猜你喜欢
  • 2021-10-24
  • 2022-12-23
  • 2022-03-03
  • 2021-11-20
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案