In testing your example above, I found that the form the GridView
is contained in gets submitted every time you click the LinkButton
. To work around this I have used the following code.
The following script will count each time the user clicks the link.
<script type="text/javascript">
var clickNo = 0;
function clickCounter() {
clickNo++;
if (clickNo == 2) {
alert("Double Click");
clickNo = 0;
}
}
</script>
We cancel the form submission so that we can track the number of times the user clicks the link. This may cause problems with your page, but appears to be the reason why the double clicks cannot be tracked.
<form id="form1" runat="server" onsubmit="return false;">
I created a template field in a GridView
control to show both the single click and double click buttons.
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="lnkBtnEdit" runat="server">LinkButton</asp:LinkButton>
<asp:LinkButton ID="lnkBtnEditDouble" runat="server">LinkButton</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
In the code behind the page we add the javascript
code for a single click and the javascript
code for a double click. Please note: the Cell reference is set to 2 whereas in your example it was 4, due to the limited columns I used in my testing.
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Please note: the value in Cells has changed for my testing data.
LinkButton btn = (LinkButton)e.Row.Cells[2].FindControl("lnkBtnEdit");
btn.Attributes.Add("onclick", "javascript:alert('Single Click');");
LinkButton btnDouble = (LinkButton)e.Row.Cells[2].FindControl("lnkBtnEditDouble");
btnDouble.Attributes.Add("onclick", "javascript:clickCounter();");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
This should allow you to capture double clicks on some links and single clicks on others. However, as mentioned above, the form submission is now cancelled and you will need to find another method to submit your data if you are to use the above code.