2014年4月20日 星期日

[ASP.NET] 在Server端動態加入Client script

說明:想在server端建立client script時,該怎麼做?


作法:
使用Page.ClientScript屬性取得的ClientScriptManager object提供的四個method:
1. RegisterStartupScript(Type type, string key, string script, bool addScriptTags):
    在網頁載入完成後及網頁OnLoad事件觸發前,會執行所加入的script,適用非一開始會用
    到的script。

2. RegisterClientScriptBlock(Type type, string key, string script, bool addScriptTags):
    會將script加入至網頁頂端(<form>下),不保證script可依註冊的順序輸出。

3. RegisterClientScriptInclude(Type type, string key, string url):
    動態加入js 檔的script,在以動態方式加入其他script前就會加入該script,因此可能無法參
    考網頁上的某些項目。

4. RegisterOnSubmitStatement(Type type, string key, string script):
    加入可回應網頁onsubmit 事件的script (ex: confirm),在送出網頁前即執行script,因此可取消送出動作。例:
    ClientScript.RegisterOnSubmitStatement(this.GetType(), "ConfirmSubmit", "return confirm('Do you want to submit the page?')");


參考資料:
http://msdn.microsoft.com/zh-tw/library/system.web.ui.page.clientscript%28v=vs.90%29.aspx
http://msdn.microsoft.com/zh-tw/library/system.web.ui.clientscriptmanager%28v=vs.90%29.aspx
http://msdn.microsoft.com/zh-tw/library/ms178207%28v=vs.100%29.aspx
http://www.dotblogs.com.tw/hatelove/archive/2009/10/28/11325.aspx
http://msdn.microsoft.com/zh-tw/library/7ytf5t7k%28v=vs.100%29.aspx


沒有留言:

張貼留言