RegisterStartupScript跟RegisterClientScriptBlock的差別

http://www.dotblogs.com.tw/jimmyyu/archive/2009/04/25/8175.aspx

先前對這兩個function有諸多誤解,後來上國外的討論區看了一下,使用RegisterStartUpScript 時,該段Script會被註冊在</from>的結尾前,而使用RegisterClientScriptBlock ,該段script會被註冊在<form>的結尾後,網頁的render機制是這樣的:

html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
 未命名頁面
</title></head>
<body>
    <form name="form1" method="post" action="RegisterTest.aspx" id="form1">
   
<script>document.getElementById('TextBox1').focus();</script>
    <div>
        <input name="TextBox1" type="text" id="TextBox1" /></div>
    <div>
    </form>
</body>
</html>

當我使用RegisterClientScriptBlock 時,由於script在元件Render出來前先被執行了,因此會出現找不到物件的錯誤,如果今天使用RegisterStartUpScript來註冊的話,script會長的像下面這樣,這時候網頁就不會有錯誤囉。

html xmlns="
http://www.w3.org/1999/xhtml" >
<head><title>
 未命名頁面
</title></head>
<body>
    <form name="form1" method="post" action="RegisterTest.aspx" id="form1">

    <div>
        <input name="TextBox1" type="text" id="TextBox1" /></div>
    <div>
    <script>document.getElementById('TextBox1').focus();</script>
    </form>
</body>
</html>

posted on 2011-08-25 10:57  CodingForever  阅读(171)  评论(0编辑  收藏  举报

导航