We may want user confirmation before the completion of the operation during the deletion and update processes in GridView control. If we consider that, especially deletion process can cause fatal errors that can’t be fixed; it would be extremely right not to allow user to trigger this kind of process by clicking a button mistakenly. We can obtain user confirmation for Update and Delete commands by doing two very simple changes in GridView control.
At first step, we need to convert the CommandField which contains Update and Delete buttons to the TemplateField. In this way, we can access Update and Delete buttons and we can add JavaScript code to the necessary client event. For this, we should first click Edit Columns link from smart tag icon of GridView, then select CommanField column in the new window and click Convert this field into a TemplateField link which is below the Properties window that is positioned in the right. By this way, we can have access to the control definitions of buttons like Update, Delete and Cancel from the HTML codes.
At second step, we need to add a small JavaScript code to OnClientClick event of the button. Thus, confirmation process will be done directly in client side instead of passing to server side and an unnecessary postback process will be prevented. The code snippet below shows how to request confirmation from the user when Delete button is clicked.
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Değiştir"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete" Text="Sil" OnClientClick="return confirm('Silmek istediğinizden emin misiniz?');"></asp:LinkButton>
</ItemTemplate>
GridView can become useful to some extent after these simple two processes. The result can be seen below…