2011-11-11 7 views
4

私はJavaScriptファイルを埋め込んだサーバーコントロールを作成しました。これは正常ですが、サーバコントロールがajax UpdatePanelの中​​に置かれると、updatepanel内で非同期ポストバックがトリガされた後に動作を停止します。ここでUpdatePanelの非同期ポストバック後にJavaScriptが埋め込まれない

は、サーバーコントロールの私のコードです:

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

    ClientScriptManager clientScriptManager = Page.ClientScript; 
    const string DATE_TIME_PICKER_JS = "JQueryControls.Scripts.DateTimePicker.js"; 

    clientScriptManager.RegisterClientScriptResource(typeof(DateTimePicker), DATE_TIME_PICKER_JS); 

    if (Ajax.IsControlInsideUpdatePanel(this) && Ajax.IsInAsyncPostBack(Page)) 
    { 
     Ajax.RegisterClientScriptResource(Page, typeof(DateTimePicker), DATE_TIME_PICKER_JS); 
    } 
} 

アヤックスclass from this linkです。このコードは、非同期ポストバック上で実行される

public static void RegisterClientScriptResource(Page page, Type type, string resourceName) { 
    object scriptManager = FindScriptManager(page); 
    if (scriptManager != null) { 
     System.Type smClass = GetScriptManagerType(scriptManager); 
     if (smClass != null) { 
      Object[] args = new Object[] { page, type, resourceName }; 
      smClass.InvokeMember("RegisterClientScriptResource", 
        System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | 
        System.Reflection.BindingFlags.InvokeMethod, 
        null, null, args); 
     } 
    } 
} 

これは、UpdatePanelの内で動作するように取得する方法上の任意のアイデアは?

答えて

4

UpdatePanelsによって、新しい要素が投稿時にページに配置されます。これはもはや同じ要素ではなく、あなたのバインディングはもう存在しません。 jQueryを使用していて、イベントをまとめて結びつけている場合は、liveというAPIを使用して、hereを見つけることができます。そうでなければ、datepickersのように、一度起動し、基本的には、新しい要素がロードされたら、いくつかのjavascriptを起動する必要がある項目の機能を変更します。

function BindEvents() 
{ 
    //Here your jQuery function calls go 
} 

var prm = Sys.WebForms.PageRequestManager.getInstance(); 
prm.add_endRequest(BindEvents); 

編集:あなたのコメントに基づいて、私はDatePicker.jsは、このようなファイルになるだろう:

$(document).ready(function() { 
    // Call BindDatePicker so that the DatePicker is bound on initial page request 
    BindDatePicker(); 

    // Add BindDatePicker to the PageRequestManager so that it 
    // is called after each UpdatePanel load 
    var prm = Sys.WebForms.PageRequestManager.getInstance(); 
    prm.add_endRequest(BindDatePicker); 
}); 

// We put the actual binding of the DatePicker to the input here 
// so that we can call it multiple times. Other binds that need to happen to the 
// elements inside the UpdatePanel can be put here as well. 
var BindDatePicker = function() { 
    $('.DatepickerInput').datepicker({ 
     showAnim: 'blind', 
     autoSize: true, 
     dateFormat: 'dd-mm-yy' 
    }); 
}; 
+0

をいただき、ありがとうございますこのすべてが必要ですが、マイクロソフトに呼び出すJavaScriptのいくつかは、コールバック機能を提供抱き合わせていますフィードバックは、私はまだこれを設定する方法についてはよく分かりません。 FYIはDateTimePicker.jsファイルに加えて、同じ方法でjquery-1.6.3.min.jsとjquery-ui-1.8.16.custom.min.jsも埋め込んでいます。これまでのDateTimePicker.jsファイルの内容は次のとおりです。$(document).ready(function(){ $( '。DatepickerInput')datepicker({ showAnim: 'blind'、 autoSize:true、 dateFormat: 'dd-mm-yy' }); });詳細を教えてもらえますか? – user1041481

+0

私はそのファイルに使用する特定のコードを与えるために質問を編集しました。 – PCasagrande

+0

すごく、今働いている。 prm.add_endRequest(BindDatePicker())を変更するだけでした。 prm.add_endRequest(BindDatePicker)に; – user1041481

関連する問題