在我的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
事件,我设置它以便每当记录的状态为“已批准”时,删除LinkButton被禁用:
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?')");
}
问题是,当用户点击禁用的LinkButton时,确认弹出窗口仍会显示,但不应该,因为LinkButton已被禁用。有一点道理,因为设置弹出窗口的属性位于OnClientClick
属性上。如何使弹出窗口不显示?我尝试在后面的代码中添加OnClientClick
属性,但它不起作用。 LinkButton直接删除记录。
答案 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)
您可以在这些步骤中解决问题。
从声明html中删除OnClientClick。
在后面的代码中的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?')");
确保仅在recStatus&lt;&gt;时添加此代码“已批准”。
答案 3 :(得分:0)
而不是在模板字段中定义OnClientClick属性。 当状态为Approved时,在RowDataBound事件上添加OnClientClick属性。
lnkButton.Attributes.Add("OnClientClick","Javascript");