如何在使用JQuery显示弹出窗口时阻止对asp按钮的回发

时间:2014-09-22 17:29:41

标签: c# javascript jquery asp.net

我的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;",因为每次点击都会进行回发。如果我有多行,它只在我第一次点击时起作用,但在任何时间之后,当点击另一个或相同的按钮时,不会显示弹出窗口。

如何解决它,所以无论点击哪个按钮,都会显示弹出窗口而不进行回发?

3 个答案:

答案 0 :(得分:1)

实际上你没有出现你的方法 myfunction()的实现,如果 myfunction()方法有任何语法错误,那么 OnClientClick < / strong>事件将无效,它将回传/提交表单到服务器。

尝试从 OnClientClick 中删除调用,然后使用类选择器在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 fiddle

的示例

答案 1 :(得分:0)

将其贴在标签上或&lt;%:Html.BeginForm%&gt;标签

答案 2 :(得分:0)

OnClientClick="return myfunction(); 

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

    return false;

}

使用这样的按钮永远不会回发。