2012-04-04 55 views
1

我也碰到過,其中在部分回發一些我的jQuery已停止工作的問題。我有一個帶有兩個單選按鈕的頁面,一個用於顯示數據,另一個用於顯示圖片庫。我使用jQuery來顯示和隱藏其中一個。部分回發和jQuery

我也有一個模式彈出擴展是更新面板內。當我關閉mpe並切換回圖像庫時,一些代碼停止工作。基本上它是一個間隔切換圖像的畫廊。還有一些縮略圖可以執行相同的操作(切換時間間隔),縮略圖不起作用。

的Html

<%@ Register Src="~/User_Controls/modify_image_table.ascx" TagName="IMG" TagPrefix="uc2" %> 
<%@ Register Src="~/User_Controls/ImageLoaderUC.ascx" TagName="ImageLoader" TagPrefix="uc8" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="PageContent" runat="server"> 
<asp:UpdatePanel ChildrenAsTriggers="true" ID="Upd1" runat="server"> 
    <ContentTemplate> 

     <div class="radioSelection"> 
      <asp:RadioButtonList ID="selectionby" runat="server" Font-Bold="true" RepeatDirection="Horizontal" 
       RepeatColumns="2" CssClass="bodycopy"> 
       <asp:ListItem Text="Image Library" Selected="True" Value="Libr"></asp:ListItem> 
       <asp:ListItem Text="Image Loader" Value="Load"></asp:ListItem> 
      </asp:RadioButtonList> 
     </div> 
     <div id="mpe" style="width: 600px; padding: 5px;"> 
      <uc2:IMG ID="IMG1" cssclass="bodycopy" runat="server" /> 
     </div> 
     <div id="imgLoader"> 
      <uc8:ImageLoader ID="ImageLoader" runat="server" /> 
     </div> 
     <asp:UpdateProgress ID="upp1" runat="server" AssociatedUpdatePanelID="Upd1"> 
      <ProgressTemplate> 
       <div id="progressBackgroundFilter"> 
       </div> 
       <div id="modalPopup"> 
        &nbsp; &nbsp; Loading... 
        <img align="middle" src="../images/Ajax/loading_1.gif" /> 
       </div> 
      </ProgressTemplate> 
     </asp:UpdateProgress> 
    </ContentTemplate> 
</asp:UpdatePanel> 

的Javascript

$(document).ready(function (e) { 
    // Execute the slideShow 
    slideShow(6000); 
    thumbInt(); // Assign int to thumbnail list items 
    clearShowClass(); // Prevents multiple li having .show 

    function clearShowClass() { 
     setTimeout(timedInterval, 1000); 
    }; 

    function timedInterval() { 
     $('ul.slideshow li').not('.show').css("opacity", 0); 
     clearShowClass(); 
    } 

    $('#footer img').mouseover(

function() { 
    $(this).animate({ 
     opacity: 3.7 
    }) 
}); 

    $('#footer img').mouseout(
    function() { 

     $(this).animate({ 
      opacity: 0.7 
     }) 
    }); 

    function thumbInt() { 
     for (i = 1; i <= $('ul.slideshow li').length; i++) { 
      $('#footer .thumbnail' + i).bind('click', { iteration: i }, function (event) { 
       $('ul.slideshow li').removeClass('show').css("opacity", 0).add($('ul.slideshow li:nth-child(' + event.data.iteration + ')').addClass('show').css("opacity", 0.0).animate({ 
        opacity: 1.0 
       }, 1000)); 

       $('#footer li').removeClass('highlight').add($('#footer li:nth-child(' + event.data.iteration + ')').addClass('highlight').add($('#footer li:nth-child(' + event.data.iteration + ') img'))); 

      }); 
     }; 
    }; 
}); 


function slideShow(speed) { 

    //Set the opacity of all images to 0 
    $('ul.slideshow li').css({ 
     opacity: 0.0 
    }); 

    //Get the first image and display it (set it to full opacity) 
    $('ul.slideshow li:first').css({ 
     opacity: 1.0 
    }).addClass('show'); 


    //Get the first thumbnail and change css 
    $('#footer li:first').css({ 
     opacity: 1.0 
    }).addClass('highlight'); 


    //Call the gallery function to run the slideshow 
    var timer = setInterval('gallery()', speed); 

    //Pause the slideshow on mouse over content 
    $('#footer, ul.slideshow').hover(

function() { 
    clearInterval(timer); 
}, 

function() { 
    timer = setInterval('gallery()', speed); 
}); 
} 

function gallery() { 
    //if no IMGs have the show class, grab the first image 
    var current = ($('ul.slideshow li.show') ? $('ul.slideshow li.show') : $('#ul.slideshow li.first')); 

    //Get next image, if it reached the end of the slideshow, rotate it back to the first image 
    var next = ((current.next().length) ? ((current.next().attr('id') == 'slideshow-caption') ? $('ul.slideshow li:first') : current.next()) : $('ul.slideshow li:first')); 


    //Set the fade in effect for the next image, show class has higher z-index 
    next.css({ 
     opacity: 4.0 
    }).addClass('show').animate({ 
     opacity: 4.0 
    }, 1000); 

    // Hide the current image 
    current.animate({ 
     opacity: 0.0 
    }, 1000).removeClass('show'); 

    //if no thumbnails have the highlight class, grab the first thumbnail 
    var currentThumb = ($('#footer li.highlight') ? $('#footer li.highlight') : $('#footer li:first')); 

    var nextThumb = ($('#footer li:last').hasClass('highlight')) ? $('#footer li:nth-child(1)') : $('#footer li.highlight').next($('#footer li')); 


    nextThumb.addClass('highlight'); 
    currentThumb.removeClass('highlight'); 
} 

基本上我想知道更新面板將如何影響$(文件)。就緒?

由於

+1

@mshsayem有正確的答案。你應該移動你的評論來回答,以便它可以被接受。 – Jeremy 2012-04-04 15:23:19

回答

3

內部$(document).ready的代碼將不被再次部分回發調用;如果您需要$(document).ready函數中的某些功能,那麼我建議將這些代碼重構爲一個函數(例如initAll()),並在部分回發後,從服務器端(在您的事件處理程序中)使用ClientScriptManager.RegisterClientScriptBlock函數調用該函數,如:

... 
// last line in your partial postback handler 

ScriptManager.RegisterClientScriptBlock(Upd1,typeof(UpdatePanel),"__updp__", "initAll();", true); 
+0

我嘗試了創建一個名爲initAll()的JavaScript函數,並將其他js函數放入其作用域中,並且還在PreRender處理函數中使用了ScriptManager。但它仍然不能正常工作。只有一些我的JavaScript工作。 – 2012-04-05 08:40:40

+0

它不應該在'PreRender'處理程序中;但是在控件的事件處理程序中,在更新面板中,同樣,裏面'initAll'你應該把只有那些更新/ JavaScript的使用控件的狀態_inside_在UpdatePanel – mshsayem 2012-04-05 09:11:20

+0

好吧,我會盡力的。如果我錯了,我正在使用用戶控件並糾正錯誤,它不是從用戶控件調用父控件的好習慣。用戶控件應該與其父項無關。 – 2012-04-05 09:55:57