簡體   English   中英

如何在使用JQuery顯示彈出窗口時防止ASP按鈕上的回發

[英]How to prevent postback on asp button while displaying a popup using JQuery

我的GridView中有以下ItemTemplate

<ItemTemplate>
    <asp:Button UseSubmitBehavior="false" runat="server" ID="btnShow" CssClass="btnSearch" Text="View All" CommandName="ViewAll" OnClientClick="myfunction(); return false;" OnCommand="btnShow_Command" CommandArgument='<%#((GridViewRow)Container).RowIndex%>' />
</ItemTemplate>

對於ItemTemplate我有一個按鈕,當使用以下JQuery單擊時,它將打開一個彈出窗口:

$(document).ready(function () {
    $(".btnSearch").click(function (e) {
        e.preventDefault();
        //centering with css
        centerPopup();
        //load popup
        loadPopup();
    });
});

function myfunction() {
}

我的Command代碼背后:

protected void btnShow_Command(object sender, CommandEventArgs e)
        {
            int index = 0;

            if (e.CommandName == "ViewAll")
            {
                index = Convert.ToInt32(e.CommandArgument);

                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;
                //tbGL.Text = html;
                //upData.Update();
                //MessageBox.Show(index.ToString());
            }
        }

我添加了OnClientClick="myfunction(); return false;" 因為每次我單擊時都會進行回發。 如果我有多行,則它僅在我第一次單擊時起作用,而在以后的任何時候,單擊另一個或相同按鈕時都不會顯示彈出窗口。

我如何解決它,所以無論單擊哪個按鈕,都會顯示彈出窗口而無需回發?

實際上,您還沒有顯示方法myfunction()的實現 ,如果myfunction()方法有語法錯誤,則OnClientClick事件將為空,它將把表單回傳/提交給服務器。

嘗試從OnClientClick刪除調用,並通過使用類選擇器在click事件上的jquery 實現您的邏輯,如下所示

$(document).ready(function () {
        $(".btnSearch").click(function (e) {
            e.preventDefault();
            alert($(this).val() + " Clicked"); // you can put your method mymethod() here 
            // you can put youe popup logic here

            return false; 

        });
    });

您還可以看到js小提琴的這個例子

將其放在標記或<%:Html.BeginForm%>標記上

OnClientClick="return myfunction(); 

function myfunction(){
// you can put youe popup logic here 

    return false;

}

像這樣使用您的按鈕永遠不會回發。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM