2012-04-22 30 views
2

從jQuery UI現在可以申請.button()一個錨,一個按鈕,提交按鈕等 我有一個簡單的asp.net按鈕,看起來像這樣:點擊ASP.NET按鈕時jQuery「.button」裝飾會消失嗎?

<asp:Button ID="btnFindSearch" runat="server" Text="Search" 
         onclick="btnFindSearch_Click" /> 

然而裏面像這樣一個UpdatePanel的:

<div id="dialog" title="Select Address"> 
     <asp:UpdatePanel ID="upNewUpdatePanel" UpdateMode="Conditional" ChildrenAsTriggers="true" runat="server"> 
     <ContentTemplate> 
      <div id="search" style="width:100%; text-align:center;"> 
       <asp:TextBox ID="txtFindSearch" runat="server"></asp:TextBox> 
       <asp:Button ID="btnFindSearch" runat="server" Text="Search" 
        onclick="btnFindSearch_Click" /> 
      </div> 
     </ContentTemplate> 
     </asp:UpdatePanel> 
     </div> 

在我的jQuery我可以這樣做:

$('#btnFindSearch').button();

它給了我一個按鈕的jQuery UI外觀...真棒我想。但是,當我點擊我的按鈕時,按鈕返回到普通的舊的asp.net服務器按鈕。它就好像.button()不再適用。

這是由於它是在一個UpdatePanel內或者什麼可能導致這種事實?

+0

這裏是.button的jQuery UI的文檔以及http://jqueryui.com/demos/button/ – oJM86o 2012-04-22 22:28:27

回答

3

這可以通過jQuery livequery plugin來完成。

它可以用來爲所有元素定義一個函數,這些元素當前在dom中,以及稍後將添加wud的那些元素。這將解決大部分問題。

下面是一個例子用法:

$("#btnFindSearch").livequery(function(){$(this).button();}) 
+0

使用jquery live在哪裏?這個答案沒有多大意義。 – oJM86o 2012-04-22 22:54:12

+0

http://docs.jquery.com/Plugins/livequery – 2012-04-22 22:55:25

+0

我知道現場是什麼,但它不能應用於這種情況 - 你甚至看過我的文章嗎? – oJM86o 2012-04-22 22:56:34

6

這是因爲在按鈕上單擊一個服務器回發發生和原來的DOM元素被替換。

UpdatePanel不是特別聰明。它只是將中的所有子內容替換爲從服務器發送的新HTML(請考慮innerHTML = severStuff)。新創建的DOM元素因此「未裝飾」。需要在新的DOM元素上再次運行.button()魔法。


除非有更好的方式來處理這個問題,這我不知道,這裏是一個helpful class for inline script blocks自動處理不同的情況。這是一種混亂和「hackish」,但它確實工作得很好(我們使用修改後的版本)。

使用可能看起來像(這必須是這個更新的UpdatePanel它在部分回發工作):

<UpdatePanel ...> 
    <OtherControls ...> 
    // Put other controls first, because this will be rendered in-order 
    // (which should usually be last!) in a normal/full PostBack. 
    // It will use the appropriate "RegisterScript" for partial PostBacks. 
    <InlineScript runat="server"> 
    <script> 
     // Add .button() stuff here - note that InlineScript is last 
     // so the previous (control) DOM nodes exist already in current 
     // browser implementations, even if there is no "DOM loaded" per-se... 
     // -- or -- 
     jQuery(function ($) { 
      // Add .button() stuff here; using $.ready here might (??) 
      // actually cause the browser to update the UI first... 
      // ...but it will guarantee the DOM is completely loaded. 
     }) 
    </script> 
    </InlineScript> 
</UpdatePanel> 
+0

犯錯,但我必須有它的UpdatePanel內由於應用程序的其他部分。特別是因爲我使用的是jQuery UI對話框......如果你有一個jQuery UI對話框對象,並且允許回發對話框關閉......所以你必須在updatepanel中做這些事情。我能做些什麼來「裝飾」按鈕嗎?我應該看看'ScriptManager.RegisterClientScript'並且只是在後面的代碼中重新應用.button? – oJM86o 2012-04-22 22:33:20

+0

@ oJM86o這是一種方法。另一種方法是查看頁面請求管理器。我不確定如果在每次PostBack之後通過JavaScript進行設置,用戶界面體驗會發生什麼...... – 2012-04-22 22:34:19

+0

您提到.button()需要在新的DOM元素上再次運行......問題是點擊該按鈕正在引起回傳..我可以回想起在哪裏。按鈕(),如果我現在在服務器端(代碼頁面後面)。 – oJM86o 2012-04-22 22:34:21