zoukankan      html  css  js  c++  java
  • .Net向Page和UpdatePanel输出JS

    Page

    大家都知道向客户端输出内容的方式很多,而打多少初学者会使用Respone.Write(string)。比如:

    Respone.Write(“hello word!”);

    或输出JS

    Respone.Write("<script type='text/javascript'>alert('hello word!');</script>");

    但是,当你查看客户端源码时,你会发现,输出的内容呈现在源码的最前端,显然它破坏了HTML的格式,在某些情况下这是会影响到页面布局等效果的。

    正确的输出方式应该是:this.ClientScript.RegisterStartupScript或this.ClientScript.RegisterClientScriptBlock.

    this.ClientScript.RegisterStartupScript 是在Form开始的第一行注册脚本,后者则是在Form结尾处注册脚本。这样就不回破坏HTML得格式了,如:

    this.ClientScript.RegisterStartupScript(this.GetType(), "scriptKey", "<script type='text/javascript'>alert('hello word!');</script>")

    this.ClientScript.RegisterStartupScript(this.GetType(), "scriptKey", "alert('hello word!');",True)

    this.ClientScript.RegisterClientScriptBlock也类似。

    UpdatePanel

    当你想在UpdatePanel内输出一段JS时,运用以上方法就会得不到预期的效果。那么请看一下示例。

    有一个UpdatePanel的ID是upPn

    ScriptManager.RegisterClientScriptBlock(upPn,this.GetType(), "scriptKey", "alert('hello word!');",True)

    ScriptManager.RegisterStartupScript(upPn,this.GetType(), "scriptKey", "alert('hello word!');",True)

    这样的话,当UpdatePanel内容加载到客户端后,就会弹出“hello word!”对话框。

    这样的话,从后台输出JS就更加方便了。

  • 相关阅读:
    快速排序和随机化快排学习
    P1330 封锁阳光大学 DFS
    P2577 [ZJOI2005]午餐 状压DP
    M. Subsequence 南昌邀请赛
    P1441 砝码称重 DFS回溯+DP
    P2661 信息传递 二分图的最小环
    P1196 [NOI2002]银河英雄传说 带权并查集
    P2024 [NOI2001]食物链 并查集
    F. Shovels Shop 背包DP
    P1514 引水入城 DFS
  • 原文地址:https://www.cnblogs.com/ke10/p/2185683.html
Copyright © 2011-2022 走看看