2010-08-15 41 views
1

我在我的aspx頁面中有PrettyPhoto的問題。更新面板控件中有一個Reapeater控件。中繼器控制重複表格行:每行包含圖像,這是一個鏈接(具有rel = prettyphoto屬性)和少量鏈接按鈕(編輯,保存)。 jQuery代碼是這樣的:asp.net和jQuery(漂亮的照片)

function bindPrettyPhoto() 
{ 
    $("a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'}); 
}; 

$(document).ready(function(){ 
    bindPrettyPhoto(); 
}); 

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(bindPrettyPhoto); 

當頁面加載漂亮的照片工作正常。當我點擊一次按鈕編輯或保存漂亮的照片工作正常,但在此之後單擊下一步,單擊更新面板中的任何其他按鈕都不會引起操作。有任何想法嗎?我會很感激任何建議。

問候, 馬丁

+0

什麼時候最後一行'Sys.WebForms ...'運行? – 2010-08-15 11:49:54

+0

完成異步回發並將控件返回給瀏覽器後,會引發EndRequest事件。 – grom 2010-08-15 11:59:40

回答

0

嘗試把Sys.WebForms...調用$(document).ready()函數內。可能是因爲您在頁面上加載特定功能之前調用了它。

0

以及因爲你需要刪除這個jquery插件添加每次頁面加載時(每個帖子後面)的Div標籤。 要做到這一點eighter在js文件的功能$.fn.prettyPhoto或在您$(document).ready(); 添加以下修補程序代碼,但你應該確保你的腳本之前的jQuery插件

修復代碼必須之前在每個頁面加載運行的運行$("a[rel^='prettyPhoto']").prettyPhoto()功能:

//to remove div tag prettyPhoto adds on each page load 
$('div.pp_pic_holder').remove(); 
$('div.pp_overlay').remove(); 
$('div.ppt').remove(); 
//End remove div tag prettyPhoto adds on each page load 

所以你可以你的函數改成這樣:

function bindPrettyPhoto() 
{ 
    //to remove div tag prettyPhoto adds on each page load 
    $('div.pp_pic_holder').remove(); 
    $('div.pp_overlay').remove(); 
    $('div.ppt').remove(); 
    //End remove div tag prettyPhoto adds on each page load 
    $("a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'}); 
}; 

正如我以前說過,你也可以修復代碼添加到JS文件功能$.fn.prettyPhoto

所以版本2.5.6只是改變了功能,這(通過在函數的開頭加上固定代碼):

$.prettyPhoto = { version: '2.5.6' }; $.fn.prettyPhoto = function (settings) { 

     $('div.pp_pic_holder').remove(); 
     $('div.pp_overlay').remove(); 
     $('div.ppt').remove(); 

.../* the rest of the function*/..... 
1

你可以沿着這個線的東西:

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

    RegisterScript(); 
} 

private void RegisterScript() 
{ 
    StringBuilder sb2 = new StringBuilder(); 
    string selector = this.ClientID.ToString(); 

    if (ScriptManager.GetCurrent(this.Page).IsInAsyncPostBack) 
    { 
     sb2.AppendLine(String.Concat("Sys.Application.add_load(", selector, "_func);")); 
     sb2.AppendLine(String.Concat("function ", selector, "_func() {")); 
    } 
    else 
    { 
     sb2.AppendLine(" $(document).ready(function() {"); 
    } 

    sb2.AppendLine("$(\"a[rel^='prettyPhoto']\").prettyPhoto({ animation_speed: 'fast' });"); 
    sb2.AppendLine("}"); 

    if (!ScriptManager.GetCurrent(this.Page).IsInAsyncPostBack) 
     sb2.Append(");"); 

    if (ScriptManager.GetCurrent(this.Page).GetRegisteredClientScriptBlocks().FirstOrDefault(e => e.Key == String.Concat("PrettyPhoto", selector)) == null) 
     ScriptManager.RegisterClientScriptBlock(this, this.GetType(), String.Concat("PrettyPhoto", selector), String.Format("<script type=\"text/javascript\">{0}</script>", sb2.ToString()), false); 
} 
1

當使用一個更新面板,只有頁面的一部分回發到服務器。 jquery命令document.ready僅在整個頁面加載時(或類似的)觸發。不幸的是,每次加載頁面上的東西時,PrettyPhoto都需要初始化。

如果您使用的是更新面板,並且希望PrettyPhoto正常工作,你需要把PrettyPhoto初始化代碼的.NET AJAX裏面的「頁面加載」功能,像這樣:

function pageLoad() { 
    $("a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'}); 
} 

對於一個好的在document.readypageLoad之間的差異的文章,看看這個鏈接:

http://encosia.com/document-ready-and-pageload-are-not-the-same/