单击时的按钮属性甚至不会激发
本文关键字:按钮 属性 单击 | 更新日期: 2023-09-27 18:27:49
我使用下面的属性。添加方法来激发按钮事件,但它没有激发。
if (String.IsNullOrEmpty(txt_DiscountCode.Text))
{
btn_Submit.Attributes.Add("onclick","return confirm('Do you want to continue without Discount Code?');");
}
我在.aspx页面上的按钮是:
<asp:Button ID="btn_Submit" runat="server" Text="Submit" OnClick="btn_Submit_Click" ValidationGroup="Submit1" />
相同的代码在GridView行命令下正常工作:
protected void grid_EditUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btn_Update = (Button)(e.Row.Cells[7].FindControl("btn_Update"));
Button btn_Delete = (Button)(e.Row.Cells[6].FindControl("btn_Delete"));
if (btn_Update != null && btn_Update.Text == "Update")
{
btn_Update.Attributes.Add("onclick", "return confirm('Are you sure you want to save?');");
//lnkbtn_Save.Attributes["onclick"] = "if(!confirm('Are you sure you want to save ?')){ return false; };";
}
else if (btn_Delete != null && btn_Delete.Text == "Delete")
{
btn_Delete.Attributes.Add("onclick", "return confirm('Are you sure you want to delete User?');");
}
}
}
我猜您希望它会立即显示,但它会在用户下次单击按钮后显示。因此,您希望有条件地添加客户端事件处理程序。最简单的方法是始终添加它,但也在客户端进行IsNullOrEmpty
检查:
btn_Submit.Attributes.Add("onclick","var txt = document.getElementById('" + txt_DiscountCode.ClientID + "'); if(txt != null && txt.value == '') return confirm('Do you want to continue without Discount Code?');");