Why is LinkButton not executing the Click function from code behind

旧街凉风 提交于 2019-12-25 03:26:46

问题


I have a GridView which has these two controls:

<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>

code-behind:

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 that displays the popup:

<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>

The GridView generates multiple rows, where each row the button will have a different INDEX number to reference the session table being used to populate ResultsDiv.InnerHtml = html;.

When I click on btnShow Button it displays the alert and shows the popup with the updated ResultsDiv.InnerHtml = html; by using the code-behind for a split second and does a postback and reloads the page.

When I click 'btnShow2' LinkButton it displays the alert and shows the popup and does not do a postback. The only issue I am having is, it doesn't access the code-behind to update ResultsDiv.InnerHtml = html; so it is always displaying the same result no matter what row the button is clicked.

How do I modify my code so that it updates the ResultsDiv.InnerHtml = html; and displays the popup every time the button is clicked on any of the row and does NOT do a postback?


回答1:


If You Remove Both OnClientClick="return false;" and PostBackUrl="JavaScript:void(0);" then definitely it will postback. You can observe your HTML generated/rendered if you set both attributes with Postback event WebForm_DoPostBackWithOptions which should be something like

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>



回答2:


You have OnClientClick="return false;". That cancels the postback. To fix it, remove that attribute from your LinkButton declaration.

Also, not sure what PostBackUrl="JavaScript:void(0);" does. I've never seen someone to do that. You might try eliminating that if it's not necessary.



来源:https://stackoverflow.com/questions/26001579/why-is-linkbutton-not-executing-the-click-function-from-code-behind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!