【问题标题】:disable OnClientClick when LinkButton is disabled禁用 LinkBut​​ton 时禁用 OnClientClick
【发布时间】:2013-12-19 20:54:46
【问题描述】:

在我的 gridview 中,我包含了一个包含 LinkButton 的列,它允许您删除 LinkButton 所在的同一行上的记录:

<asp:TemplateField HeaderText="">
     <ItemTemplate>
          <asp:LinkButton ID="lnkDeleteTxn" Text="Delete" runat="server" CommandArgument='<%# Eval("TxnID") %>' OnClick="deleteTxn" 
          OnClientClick="return confirm('!!--WARNING--!! You are about to delete the transaction. Performing this action will permanently remove the transaction and all its details from the database. Proceed?');"></asp:LinkButton>
     </ItemTemplate>
</asp:TemplateField>

这个LinkButton 包括显示一个弹出窗口,询问用户是否真的要删除记录。现在,对于我的RowDataBound 事件,我将其设置为,只要记录的状态为“已批准”,删除链接按钮就会被禁用:

string recStatus = Convert.ToString(DataBinder.Eval(e.Row.DataItem,"StatusDesc"));
if (recStatus == "Approved")
{
    hlTxnEdit.Enabled = false;
    lnkTxnDelete.Enabled = false;
}
else
{
    hlTxnEdit.Enabled = true;
    lnkTxnDelete.Enabled = true;
    //lnkTxnDelete.Attributes.Add("OnClientClick", "return confirm('!!--WARNING--!! You are about to delete the transaction. Performing this action will permanently remove the transaction and all its details from the database. Proceed?')");
}

问题是,当用户单击禁用的 LinkBut​​ton 时,仍会显示确认弹出窗口,但它不应该显示,因为 LinkBut​​ton 已禁用。有点道理,因为设置弹窗的Attribute在OnClientClick属性上。如何使弹出窗口不显示?我尝试在后面的代码中添加OnClientClick 属性,但它不起作用。 LinkBut​​ton 直接删除记录。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:
    if (recStatus == "Approved")
     {
        hlTxnEdit.Enabled = false;
        lnkTxnDelete.onClientClick = null;//
    
     }
     else
      {
    
        hlTxnEdit.Enabled = true;
        lnkTxnDelete.Enabled = true;
        //lnkTxnDelete.Attributes.Add("OnClientClick", "return confirm('!!--WARNING--!! You are about to delete the transaction. Performing this action will permanently remove the transaction and all its details from the database. Proceed?')");
      }
    

    【讨论】:

      【解决方案2】:

      如下更改您的aspx...

      <asp:TemplateField HeaderText="">
          <ItemTemplate>
              <asp:LinkButton ID="lnkDeleteTxn" Text="Delete" runat="server" CommandArgument='<%# Eval("TxnID") %>' OnClick="deleteTxn"></asp:LinkButton>
          </ItemTemplate>
      </asp:TemplateField>
      

      像这样从后面的代码中添加您的确认弹出窗口。

      string recStatus = Convert.ToString(DataBinder.Eval(e.Row.DataItem,"StatusDesc"));
      if (recStatus == "Approved")
      {
          hlTxnEdit.Enabled = false;
          lnkTxnDelete.Enabled = false;
          lnkTxnDelete.Attributes.Add("onclick", "return false;");
      }
      else
      {
          hlTxnEdit.Enabled = true;
          lnkTxnDelete.Enabled = true;
          lnkTxnDelete.Attributes.Add("onclick", "return confirm('!!--WARNING--!! You are about to delete the transaction. Performing this action will permanently remove the transaction and all its details from the database. Proceed?')");
      }
      

      【讨论】:

      • 我不能使用它,因为我已经在使用onclick 属性。我的onclick 属性调用了一个删除记录的函数。
      • @JamesP,这将是客户端点击。试一试,它应该可以在不干扰服务器端点击事件的情况下工作。
      • 嗯,我试过了,结果是,当记录的状态为NOT Approved时,删除LinkBut​​ton什么也不做。就像,真的没什么。
      • 是的,@JamesP!我通过从aspx 中删除OnClientClick 并在RowDataBound 事件中添加return false; 部分来解决此问题。
      【解决方案3】:

      而不是在模板字段中定义 OnClientClick 属性。 当状态为 Approved 时,在 RowDataBound 事件上添加 OnClientClick 属性。

      lnkButton.Attributes.Add("OnClientClick","Javascript");  
      

      【讨论】:

        【解决方案4】:

        您可以通过以下步骤解决此问题。

        1. 从声明 html 中删除 OnClientClick。

        2. On GridView OnDataRowBound 事件处理函数在后面的代码中添加这个 OnClientClick

          lnkTxnDelete.Attributes.Add("OnClientClick", "return confirm('!!--WARNING--!! You are about to delete the transaction. Performing this action will permanently remove the transaction and all its details from the database. Proceed?')");
          
        3. 确保仅在 recStatus “已批准”时添加此代码。

        【讨论】:

        • 请看上面的示例代码。我已经尝试在后面的代码中添加OnClientClick 属性,但它不起作用。我把它注释掉了,因为它不起作用。而且,您在第 3 步中的建议与应该做的事情相反。当 recStatus 为 Approved 时不应该添加它,因为在我的系统中,如果记录的状态为 "Approved" ,则您不能删除或编辑该记录。
        • 好吧,我弄错了。您必须将其添加到“其他”部分。如果您仍然遇到问题,请与我联系,我可以为您解决此问题。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-27
        • 1970-01-01
        • 2011-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多