zoukankan      html  css  js  c++  java
  • 【转】asp.net向客户端注册JavaScript脚本的三种方法

    一般在sap.net中向客户端注册脚本有三种方法

    1.使用Literal控件在页面的任意位置注册脚本

    2.使用Response.Write()在页面的顶部注册脚本

    3.使用ClientScript.RegisterClientScriptBlock()或者ClientScript.RegisterStartupScript()分别在表单开始和结束的地方注册脚本

    下面给出一个使用了这三种方法的例子,新建一个apsx文件ScriptDemo.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ScriptDemo.aspx.cs" Inherits="ScriptDemo" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Literal ID="LiteralScript" runat="server"></asp:Literal></div>
        </form>
    </body>
    </html>
    可以看到我们在页面中仅仅放置了一个Literal控件,下面是.cs文件ScriptDemo.aspx.cs

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class ScriptDemo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("<script>alert('使用Response.Write()');</script>");
            Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "", "<script>alert('使用Page.ClientScript.RegisterClientScriptBlock()');</script>");
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>alert('使用Page.ClientScript.RegisterStartupScript()');</script>");
            LiteralScript.Text += "<script>alert('使用Literal控件');</script>";
        }
    }
    在这里面我们使用了三种向客户端注册脚本的方法,在这个例子中脚本的调用顺序应该是

    Response.Write()----->Page.ClientScript.RegisterClientScriptBlock()--->使用Literal控件

    -->Page.ClientScript.RegisterStartupScript()

    运行程序,在浏览器中查看源文件如下:

    <script>alert('使用Response.Write()');</script>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head><title>
    无标题页
    </title><link href="App_Themes/Blue/css.css" type="text/css" rel="stylesheet" /></head>
    <body>
        <form name="form1" method="post" action="ScriptDemo.aspx" id="form1">
    <div>
    <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTYyNzcxNDY4Ng9kFgICAw9kFgICAQ8WAh4EVGV4dAUuPHNjcmlwdD5hbGVydCgn5L2/55SoTGl0ZXJhbOaOp+S7ticpOzwvc2NyaXB0PmRkbmbYaQhVe15xJuXdkOcOasDLc30=" />
    </div>

    <script>alert('使用Page.ClientScript.RegisterClientScriptBlock()');</script>
        <div>
            <script>alert('使用Literal控件');</script></div>
       
    <script>alert('使用Page.ClientScript.RegisterStartupScript()');</script></form>
    </body>
    </html>
    通过源文件我们可以验证刚才的说法,同时我们也看出,Reponse.Write()方法输出的代码是在整个页面最顶部的,Page.ClientScript.RegisterClientScriptBlock()注册的脚本则是出现在表单的最顶部,Page.ClientScript.RegisterStartupScript()注册的脚本出现在表单的最底部,使用Literal控件输出的脚本就在Literal控件所在位置处

  • 相关阅读:
    GPU
    Windows系统之hosts文件
    条形码定位算法
    entity framework extended library , bulk execute,deleting and updating ,opensource
    sharepoint 2013 sp1
    windows azure programing
    windows azure tools for mac
    online web design tool
    toastr
    sharepoint online
  • 原文地址:https://www.cnblogs.com/yuanyuan/p/1495564.html
Copyright © 2011-2022 走看看