我正在研究一个asp.net Webforms项目。我在网格视图中动态创建按钮。单击时,应显示一个JavaScript确认弹出框。当用户单击“确定”按钮时,它应该转到按钮单击事件并执行数据库更新。否则,什么都不做。代码如下。大多数情况下,它都能正常工作。但是有时,没有到达背后的代码。当我第二次单击按钮时,它将回发。我尝试了Implementing javascript confirm box in code behind C#提供的解决方案,并发现了类似的行为。
Button being created dynamically:
Button btnMarkSaved = new Button();
btnMarkSaved .CssClass = "ArrangeButtons grayButtonStyle";
btnMarkSaved .Height = Unit.Pixel(30);
btnMarkSaved .Text = "Mark as Saved";
btnMarkSaved .CausesValidation = false;
btnMarkSaved .ID = "btnMarkSaved_" + clientId + "_" + orderId + "_" + storeId;
this.btnMarkSaved.Click += new System.EventHandler(this.btnMarkSaved_Click);
this.btnMarkSaved.OnClientClick = "return confirm('Are you sure to tag the issue
as saved?');";
Code behind method:
protected void btnMarkSaved_Click(Object sender, System.EventArgs e)
{
String[] btnIDFields;
btnIDFields = btn.ID.Split('_');
UpdateOrderHistory(btnIDFields[2]);
}