为什么LinkBut​​ton没有从代码后面执行Click函数

时间:2014-09-23 17:52:14

标签: c# jquery html asp.net sharepoint

我有一个GridView,它有两个控件:

<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;弹出窗口并进行回发并重新加载页面。

当我点击&#39; btnShow2&#39; LinkBut​​ton它显示警报并显示弹出窗口并且不进行回发。我遇到的唯一问题是,它无法访问代码隐藏更新ResultsDiv.InnerHtml = html;,因此无论单击按钮的哪一行,它始终显示相同的结果。

如何修改我的代码以便更新ResultsDiv.InnerHtml = html;并在每次点击按钮时显示弹出窗口并且不进行回发?

2 个答案:

答案 0 :(得分:1)

如果删除两者 OnClientClick="return false;"和  PostBackUrl="JavaScript:void(0);"然后肯定会回发。 如果使用Postback事件设置这两个属性,则可以观察生成/呈现的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>

答案 1 :(得分:0)

你有OnClientClick="return false;"。这取消了回发。要解决此问题,请从LinkBut​​ton声明中删除该属性。

另外,不确定PostBackUrl="JavaScript:void(0);"的作用。我从来没有见过有人这样做过。如果没有必要,你可以尝试消除它。