简体   繁体   English

如何在使用JQuery显示弹出窗口时防止ASP按钮上的回发

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

I have the following ItemTemplate in my GridView: 我的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>

For the ItemTemplate I have a button which opens a popup window when clicked by using the following JQuery: 对于ItemTemplate我有一个按钮,当使用以下JQuery单击时,它将打开一个弹出窗口:

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

function myfunction() {
}

my Command code-behind: 我的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());
            }
        }

I added the OnClientClick="myfunction(); return false;" 我添加了OnClientClick="myfunction(); return false;" because it was doing a postback each time I clicked. 因为每次我单击时都会进行回发。 If I have multiple rows, it only works the first time I click but any time after, the popup is not displayed when another or the same button is clicked. 如果我有多行,则它仅在我第一次单击时起作用,而在以后的任何时候,单击另一个或相同按钮时都不会显示弹出窗口。

How do I resolve it so no matter which button is clicked the popup is displayed without doing a postback? 我如何解决它,所以无论单击哪个按钮,都会显示弹出窗口而无需回发?

Actually you have not showed up the implementation of your method myfunction() , in case the myfunction() method have any syntactical error then the OnClientClick event will be void and it will post-back/submit the form to the server. 实际上,您还没有显示方法myfunction()的实现 ,如果myfunction()方法有语法错误,则OnClientClick事件将为空,它将把表单回传/提交给服务器。

Try to remove the call from OnClientClick and just implement your logic at jquery on click event by using class selector as follows 尝试从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; 

        });
    });

You can also see this example of js fiddle 您还可以看到js小提琴的这个例子

将其放在标记或<%:Html.BeginForm%>标记上

OnClientClick="return myfunction(); 

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

    return false;

}

Using like this your button never do post back. 像这样使用您的按钮永远不会回发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM