2012-10-16 21 views
0

如何在尚未呈現在頁面上的控件上連接jQuery?我嘗試覆蓋以下方法,但它們不起作用,因爲StepNavigationTemplateContainerID html標記尚未在此事件上提供:Render,OnLoad,OnInit等嚮導的下一步按鈕接線jQuery

難道是因爲我使用UpdatePanel?當我使用UpdatePanel時,我可以重寫什麼方法/事件,以便我可以在那裏發出jQuery鉤子?它使用.on()

protected override void OnLoad(EventArgs e) 
{ 
    base.OnLoad(e); 

    HookNextButton(); 
}  



void HookNextButton() 
{ 
    string nextButtonClientId = wizardCooking.FindControl("StepNavigationTemplateContainerID").FindControl("btnNextStep").ClientID + "_lnkButton"; 
    var scriptKey = "Yo"; 

    string theScript = 
     string.Format(
    @" 
        $(function() {{  


         $('#{0}').click(function() {{ 
          alert('hi'); 

         }}); 

        }}); 
       ", nextButtonClientId); 
    Type cstype = this.GetType(); 

    // Get a ClientScriptManager reference from the Page class. 
    ClientScriptManager cs = Page.ClientScript; 

    // Check to see if the startup script is already registered. 
    if (!cs.IsStartupScriptRegistered(cstype, scriptKey)) 
    { 
     ScriptManager.RegisterStartupScript(
       this, this.GetType(), scriptKey, theScript, true); 
    } 


} 

回答

3

代表.. 這將確保該事件關聯到哪個將被插入到在稍後的時間的DOM元素..

$('body').on('click' , '#buttonid' , function() { 
    // Your code here 
    // This will handle the case of buttons that 
    // will be rendered later 

}); 
+0

「上」 [沒有按」 t工作](http://jsfiddle.net/D7vMn/1/)。 '活'[作品](http://jsfiddle.net/D7vMn/2/)雖然。雖然有很多[缺點](http://stackoverflow.com/questions/8042576/whats-the-difference-between-jquery-live-and-on)。我寧願在UpdatePanel的ajax期間發生它。我會upvote你,雖然 –

+0

什麼版本的jquery你使用的是..如果它的版本比1.7更早嘗試使用委託(),而不是 –

相關問題