c#
  • jquery
  • html
  • asp.net
  • sharepoint
  • 2014-09-23 51 views 0 likes 
    0

    點擊功能我有一個GridView它有這兩個控件:爲什麼LinkBut​​ton的不執行的代碼背後

    <asp:Button UseSubmitBehavior="false" runat="server" ID="btnShow" CssClass="btnSearch" Text="View All" CommandName="ViewAll" CommandArgument='<%#((GridViewRow)Container).RowIndex%>' OnClick="btnShow_Click" /> 
    
    <asp:LinkButton runat="server" ID="btnShow2" CssClass="btnSearch2" Text="View Allst" CommandName="ViewAll" CommandArgument='<%#((GridViewRow)Container).RowIndex%>' PostBackUrl="JavaScript:void(0);" OnClientClick="return false;" OnClick="btnShow_Click">View Alls</asp:LinkButton> 
    

    後臺代碼:

    protected void btnShow_Click(object sender, EventArgs e) 
    { 
        System.Web.UI.WebControls.Button btn1 = (System.Web.UI.WebControls.Button)(sender); 
        string strCA = btn1.CommandArgument; 
        string strCN = btn1.CommandName; 
        int index = 0; 
    
        if (strCN == "ViewAll") 
        { 
         index = Convert.ToInt32(strCA); 
    
         DataTable cacheTable = HttpContext.Current.Cache["ResultsTable"] as DataTable; 
    
         string column = cacheTable.Rows[index].Field<string>("Guideline"); 
         string test = BookingResults.Rows[index].Cells[7].Text; 
         string html = HttpUtility.HtmlDecode(column); 
    
         ResultsDiv.InnerHtml = html; 
        } 
    } 
    

    JQuery的:

    $(document).ready(function() { 
    
        //Click the button event! 
        $(".btnSearch").click(function (e) { 
         e.preventDefault(); 
         alert($(this).val() + " Clicked"); 
    
         //centering with css 
         centerPopup(); 
         //load popup 
         loadPopup(); 
        }); 
    
        $(".btnSearch2").click(function (e) { 
         e.preventDefault(); 
         alert($(this).val() + " Clicked"); 
    
         //centering with css 
         centerPopup(); 
         //load popup 
         loadPopup(); 
        }); 
    
        $("#popupContactClose").click(function() { 
         disablePopup(); 
        }); 
    
        $("#backgroundPopup").click(function() { 
         disablePopup(); 
        }); 
    
        //Press Escape event! 
        $(document).keypress(function (e) { 
         if (e.keyCode == 27 && popupStatus == 1) { 
          disablePopup(); 
         } 
        }); 
    }); 
    
    var popupStatus = 0; 
    
    //loading popup with jQuery magic! 
    function loadPopup() { 
        //loads popup only if it is disabled 
        if (popupStatus == 0) { 
         $("#backgroundPopup").css({ 
          "opacity": "0.7" 
         }); 
         $("#backgroundPopup").fadeIn("slow"); 
         $("#popupContact").fadeIn("slow"); 
         popupStatus = 1; 
        } 
        alert(popupStatus); 
    } 
    
    //disabling popup with jQuery magic! 
    function disablePopup() { 
        //disables popup only if it is enabled 
        if (popupStatus == 1) { 
         $("#backgroundPopup").fadeOut("slow"); 
         $("#popupContact").fadeOut("slow"); 
         popupStatus = 0; 
        } 
        alert(popupStatus); 
    } 
    
    //centering popup 
    function centerPopup() { 
        //request data for centering 
        var windowWidth = document.documentElement.clientWidth; 
        var windowHeight = document.documentElement.clientHeight; 
        var popupHeight = $("#popupContact").height(); 
        var popupWidth = $("#popupContact").width(); 
        //centering 
        $("#popupContact").css({ 
         "position": "absolute", 
         "top": windowHeight/2 - popupHeight/2, 
         "left": windowWidth/2 - popupWidth/2 
        }); 
        //only need force for IE6 
    
        $("#backgroundPopup").css({ 
         "height": windowHeight 
        }); 
    } 
    

    顯示彈出式菜單的HTML:

    <div id="popupContact"> 
         <a id="popupContactClose" title="Close Window">x</a> 
         <h3>Booking Guidelines</h3> 
         <asp:Panel ID="Panel1" runat="server" style="vertical-align:top" ScrollBars="Vertical" Height="300px" ForeColor="Black"> 
         <div id="ResultsDiv" runat="server" style="vertical-align:top" > </div> 
         </asp:Panel> 
    </div> 
    <div id="backgroundPopup"></div> 
    

    GridView生成多行,其中每行按鈕將具有不同的INDEX編號以引用用於填充ResultsDiv.InnerHtml = html;的會話表。

    當我點擊btnShow按鈕時,它會顯示警報,並通過使用代碼隱藏分秒來顯示彈出式對話框ResultsDiv.InnerHtml = html;,並執行回發並重新加載頁面。

    當我點擊「btnShow2」LinkBut​​ton時,它會顯示警報並顯示彈出窗口並且不會執行回發。我遇到的唯一問題是,它不訪問代碼隱藏以更新ResultsDiv.InnerHtml = html;,因此無論按鈕被點擊的行如何都始終顯示相同的結果。

    如何修改我的代碼,使其更新ResultsDiv.InnerHtml = html;,並在每次單擊任何一行上的按鈕時顯示彈出窗口並且不執行回發?

    回答

    1

    如果你刪除兩個 OnClientClick="return false;"PostBackUrl="JavaScript:void(0);"那麼肯定會回發。 您可以觀察生成/渲染你的HTML,如果你設置與回發事件這兩個屬性 WebForm_DoPostBackWithOptions這應該是這樣的

    javascript:__doPostBack('BookingResults$ctl02$btnShow2','') 
    
    <a href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;BookingResults$ctl03$btnShow2&quot;, &quot;&quot;, false, &quot;&quot;, &quot;JavaScript:void(0);&quot;, false, true))" class="btnSearch2" id="BookingResults_btnShow2_1">View Alls</a> 
    
    +0

    謝謝。我如何防止回發? – SearchForKnowledge 2014-09-23 18:27:13

    +0

    使用Update Panel或AJAX調用jquery – 2014-09-23 18:30:53

    +0

    我應該在'UpdatePanel'中放置'ResultsDiv' DIV和'GridView'嗎? – SearchForKnowledge 2014-09-23 18:32:57

    0

    您有OnClientClick="return false;"。這取消了回發。要修復它,請從您的LinkBut​​ton聲明中刪除該屬性。

    此外,不知道PostBackUrl="JavaScript:void(0);"做什麼。我從來沒有見過有人這樣做。如果沒有必要,你可以嘗試消除。

    +0

    我刪除它,它不訪問代碼仍然落後:/我有一個斷點在Click事件中,只有'Button'激發了該函數,但LinkBut​​ton沒有。 JQuery適用於兩者。如果它有幫助,它是一個可視化的Web部件。 – SearchForKnowledge 2014-09-23 17:58:57

    +0

    LinkBut​​ton甚至不會觸發Click方法! :/它是在一個GridView – SearchForKnowledge 2014-09-23 18:02:19

    相關問題