【问题标题】:Creating Confirmation message when a Delete button clicked on Asp.Net Page在 Asp.Net 页面上单击删除按钮时创建确认消息
【发布时间】:2012-11-19 11:52:30
【问题描述】:

我将此代码用于确认消息的删除按钮:

<asp:Button runat="server" ID="btnDelete" 
 OnClick="btnDelete_Click" 
 OnClientClick="return confirm('Do you want to delete the record ? ');" />

这是在服务器端添加确认客户端脚本的方法:

btnDelete.Attributes.Add("onclick", 
    "return confirm('Do you want to delete the record ? ');")

但是我在调​​试时遇到(预期类型)错误,有人可以帮忙吗? 谢谢

【问题讨论】:

  • 这是你第一次提到btnDelete吗?你如何声明它? btnDelete 是否嵌套在另一个控件中?
  • 当我复制粘贴并测试您的代码时,我没有收到任何错误!是否有任何其他脚本调用引发该错误?
  • 我猜按钮是嵌套的,否则内联 onClientClick 处理程序将简单地工作。

标签: c# asp.net web-applications


【解决方案1】:

如果您已分配 OnClientClick="return confirm('Do you want to delete the record ? ');" 在您的标记中您不需要再次添加属性。这可能是原因。

从服务器注释掉

//btnDelete.Attributes.Add("onclick", 
    "return confirm('Do you want to delete the record ? ');")

【讨论】:

    【解决方案2】:

    您拥有的客户端标记是正确的并且必须有效。

    如果您想从服务器端生成 JavaScript 代码,请删除相关标记 OnClientClick。您正在重复客户端事件处理。

    注意:请考虑在页面加载时使用附加事件(即:使用 jquery),而不是“onclick”属性。

    例子:

    $(".btn-delete").click(function() { /*do something here*/ });
    

    【讨论】:

      【解决方案3】:

      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

          {
              {
                  if (e.Row.RowType == DataControlRowType.DataRow)
                  {
                      foreach (Button button in e.Row.Cells[12].Controls.OfType<Button>())
      
                      {
                          if (button.CommandName == "Delete")
                          {
                              button.Attributes["onclick"] = "if(!confirm('Do you want to delete')){return false;};";
                          }
      
                      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-03
        • 2018-01-06
        • 1970-01-01
        • 2020-10-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多