2010-09-17 28 views
3

這是我以前試過這樣解決的問題,但放棄了。 基本上我使用ModalPopupExtenders(來自AJAX.NET)來顯示具有一些內容(文本,控件等)的面板。我從代碼隱藏中調用它。它運作良好。ASP.NET和jQuery - 來自代碼隱藏的調用

但是現在我想用一些jQuery對話框替換ModalPopup。問題是從代碼隱藏中調用它。 據我所知,我必須在RegisterStartup事件上註冊jQuery庫,但我試過了,並從代碼隱藏中調用jQuery,但沒有成功。

有人可以幫助我嗎?我真的想替換ModalPopup,他們給了我很多麻煩。

在此先感謝。


protected void Page_Load(object sender, EventArgs e) 
{ 
    ScriptManager.RegisterStartupScript(this, GetType(), "registerDialog", 
     "$(function() { $('#dialog').dialog({autoOpen:false, show:'blind'}); });", true); 
} 

protected void Button1_Click(object sender, EventArgs e) 
{ 
    ScriptManager.RegisterStartupScript(this, GetType(), "openDialog", 
     "$('#dialog').dialog('open');", true); 
} 

這是正確的方法是什麼?我必須先註冊才能隱藏。 Thanksю

回答

4

如果你使用一個ScriptManager,使用RegisterStartupScript(),像這樣:

ScriptManager.RegisterStartupScript(this, GetType(), "modalscript", 
    "$(function() { $('#dialog').dialog(); });", true); 

如果你使用一個ScriptManager/UpdatePanel的是,使用the equivalent ClientScriptManager version

重要的是要記住將代碼包裝在document.ready處理程序中(IE沒有它的問題最多),所以您的元素(在我的示例中爲id="dialog")位於DOM中並準備就緒。

+0

謝謝尼克。 當你說我的代碼包裝在一個document.ready中,在示例中,我已經展示過了,我從ScriptManager.RegisterStartupScript中做了所有的事情。 有什麼我應該改變的嗎?我雖然在document.ready中寫了registerDialog,但我不知道它是否相同。 – 2010-09-17 10:41:14

+0

@Guilherme - 將'this'(第一個參數)更改爲您所在的UpdatePanel(如果是這種情況,並且您沒有在您的RegisterStartupScript調用中使用'$(function(){});'wrapper,make肯定要這樣做:) – 2010-09-17 10:44:32

+0

再次感謝,解決了;) – 2010-09-17 10:58:11

1

你實際上並沒有從後面的代碼中調用jQuery,你只是寫了一些額外的JavaScript代碼在頁面加載(在回發之後)上運行。

在此啓動代碼中,您將調用jQuery。