禁用LinkBut​​ton时禁用OnClientClick

时间:2013-12-18 07:20:40

标签: c# asp.net

在我的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事件,我设置它以便每当记录的状态为“已批准”时,删除LinkBut​​ton被禁用:

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已被禁用。有一点道理,因为设置弹出窗口的属性位于OnClientClick属性上。如何使弹出窗口不显示?我尝试在后面的代码中添加OnClientClick属性,但它不起作用。 LinkBut​​ton直接删除记录。

4 个答案:

答案 0 :(得分:1)

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?')");
}

答案 1 :(得分: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 :(得分:0)

您可以在这些步骤中解决问题。

  1. 从声明html中删除OnClientClick。

  2. 在后面的代码中的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&lt;&gt;时添加此代码“已批准”。

答案 3 :(得分:0)

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

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