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

我的GridView中有以下ItemTemplate

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

对于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("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中删除调用,并通过使用类选择器在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; } 

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