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