動態輸出javascript指令碼[ClientScriptManager類]

來源:互聯網
上載者:User

在上篇“ASP.NET頁面的聲明周期”中,我們查看該頁面的源碼可以看到如下情況:

頁面顯示的文字被顯示在<html>和</html>之外,這不符合XHTML標準。這對於普通頁面來說也許並無大礙,但是如果在頻繁輸出javascript指令碼的網頁中,可能會對網頁的用戶端執行效果產生影響。因為javascript指令碼塊在用戶端調用方法之前還是用戶端調用方法之後效果可能會不一樣。
建立一個aspx檔案,名為Home,在Page_Load事件中添加代碼,如下:

輸出javascript

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Response.Write("<script lauguage='javasrcipt'>alert('"+DateTime.Now.ToString()+"')</script>");
        }
    }

 

這樣每次運行Home.aspx頁面的時候都會彈出一個顯示目前時間的對話方塊。但當查看其原始碼時會發現:

查看源碼

<script lauguage='javasrcipt'>alert('2010/8/13 23:16:23')</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>
</head>
<body>
    <form method="post" action="Home.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZPJPFVG/mvEj7wJdj697BaFPCzi8KIMu+OvQgQLstVMC" />
</div>
    <div>
    
    </div>
    </form>
</body>
</html>

 

可以看見輸出的javascript代碼在<html></html>標記之外。

解決辦法:

在Page類中有一個ClientScript屬性,它是ClientScriptManager的執行個體,這個類是在asp.net2.0中新增的。

ClientScriptManager有如下幾個常用方法:

RegisterClientScriptBlock方法:向 Page 對象註冊用戶端指令碼。
RegisterStartupScript方法:向 Page 對象註冊啟動指令碼。
ClientScriptManager類通過鍵string和Type來唯一標識指令碼。具有相同類型的鍵和Type的指令碼識為同一指令碼。

下面對Home表單的Page_Load事件修改代碼如下:

ClientScript兩個方法

protected void Page_Load(object sender, EventArgs e)
    {
        if (!ClientScript.IsClientScriptBlockRegistered(this.GetType(), "ClientScriptBlock"))
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "ClientScriptBlock", "<script lauguage='javascript'>alert('ClientScriptBlock')</script>");
        }
        if(!ClientScript.IsStartupScriptRegistered(this.GetType(),"StartupScript"))
        {
            ClientScript.RegisterStartupScript(this.GetType(),"StartupScript","<script lauguage='javascript'>alert('StartupScript')</script>");
        }
    }

 

執行該頁面時,會彈出兩個提示視窗,產生的HTML代碼如下:

ClientScript產生HTML

<!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>
</head>
<body>
    <form method="post" action="Home.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZPJPFVG/mvEj7wJdj697BaFPCzi8KIMu+OvQgQLstVMC" />
</div>
 
<script lauguage='javascript'>alert('ClientScriptBlock')</script>
    <div>
    
    </div>
    
<script lauguage='javascript'>alert('StartupScript')</script></form>
</body>
</html>

 

可以看出上面的兩個方法輸出的javascript指令碼都在<form></form>標記之內,不會破環文章的結構,而且RegisterClientScriptBlock方法輸出的javascript指令碼代碼塊靠近<form>標記的開始標記,而RegisterStartupScript方法輸出的javascript指令碼代碼塊靠近<form>標記的結束標記,瞭解這一點對於控制動態添加的用戶端指令碼的時間是非常有利的。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.