gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 11:47:45 25 4
gpt4 key购买 nike

我的 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() {
}

我的命令代码隐藏:

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 中删除调用,并使用 类选择器 在 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 的示例

关于c# - 如何在使用 JQuery 显示弹出窗口时防止 asp 按钮上的回发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25979847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com