Reputation: 195
In my gridview, I have included a column containing a LinkButton
that allows you to delete the record on the same row where the LinkButton
is :
<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>
This LinkButton
includes showing a pop-up window asking if the user really wants to delete the record. Now, for my RowDataBound
event, I set it so that whenever the Status of the Record is "Approved", the Delete LinkButton is disabled:
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?')");
}
the problem is, when the user clicks on the disabled LinkButton, the confirmation pop-up still displays, when it should not, because the LinkButton is disabled. Makes a bit of sense, because the Attribute where the pop-up window is set is on the OnClientClick
attribute. How do I make the pop-up window not show up? I tried adding the OnClientClick
attribute in the code behind, but it doesn't work. The LinkButton proceeds straight on deleting the record.
Upvotes: 1
Views: 11335
Reputation: 369
You can fix the issue in these steps.
Remove the OnClientClick from Declaration html.
On GridView OnDataRowBound event handler in the code behind add this 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?')");
Make sure you are adding this code only when recStatus <> "Approved".
Upvotes: 0
Reputation: 3611
Make change in your aspx
as below...
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID="lnkDeleteTxn" Text="Delete" runat="server" CommandArgument='<%# Eval("TxnID") %>' OnClick="deleteTxn"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Add your confirmation pop-up from the code behind like this.
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?')");
}
Upvotes: 1
Reputation: 4069
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?')");
}
Upvotes: 5
Reputation: 120
Rather than defining the OnClientClick attribute in template field. Add the OnClientClick attribute on your RowDataBound event when status is Approved.
lnkButton.Attributes.Add("OnClientClick","Javascript");
Upvotes: 0