title: How to display confirmation message with GridView ShowDeleteButton CommandField
body: <p>I'm trying to attach a Javascript function to confirm a delete of a record in a GridView. I know this can be done more easily using an asp:LinkButton in an ItemTemplate but I am trying to attach it to a CommandField - ShowDeleteButton button. </p>
  
  <p>I've tried to follow this tutorial: <a href="http://geekswithblogs.net/dotNETvinz/archive/2009/02/22/display-confirmation-message-on-gridview-deleting.aspx" rel="nofollow noreferrer">display-confirmation-message-on-gridview-deleting</a></p>
  
  <p>I am new to GridView and my implementation is in VB.Net not C#. Here is the code where I try to inject the Javascript function and I cannot figure out how/why my row.cell references are wrong:</p>
  
  <pre class="lang-vb prettyprint-override"><code>Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) _
      Handles GridView1.RowDataBound
      If e.Row.RowState &lt;&gt; DataControlRowState.Edit Then
          If e.Row.RowType = DataControlRowType.DataRow Then  'check for RowType DataRow
              Dim id As String = e.Row.Cells(0).Text      'get the position to be deleted
             Try
              'cast the ShowDeleteButton link to linkbutton
              Dim lb As LinkButton
              Dim i As Integer = 4            'cell we want in the row (relative to 0 not 1) 
              lb = DirectCast(e.Row.Cells(i).Controls(2), LinkButton)
              If lb IsNot Nothing Then        'attach the JavaScript function with the ID as the parameter
                  lb.Attributes.Add("onclick", "return ConfirmOnDelete('" &amp; id &amp; "');")
              End If
             Catch ex As Exception
             End Try
          End If
      End If
  </code></pre>
  
  <p>Here is my GridView markup snippet (a bit more busy than all bound columns; I count 3 TemplateField items after the first and only BoundField to arrive at the 5th column hence i = 4 above):</p>
  
  <pre><code>&lt;Columns&gt;
      &lt;asp:BoundField DataField="PositionID" HeaderText="ID" ReadOnly="true" /&gt;
      &lt;asp:TemplateField HeaderText="PositionTitle"&gt;
          &lt;ItemTemplate&gt;
              &lt;%# Eval("PositionTitle")%&gt;
          &lt;/ItemTemplate&gt;
          &lt;EditItemTemplate&gt;
              &lt;asp:TextBox runat="server" ID="txtPositionTitle" Text='&lt;%# Eval("PositionTitle")%&gt;' /&gt;
          &lt;/EditItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
      &lt;asp:TemplateField HeaderText="Incumbent"&gt;
          &lt;ItemTemplate&gt;
              &lt;asp:Label ID="lblUser" runat="server" Text='&lt;%# Eval("Incumbent")%&gt;'&gt;&lt;/asp:Label&gt;
          &lt;/ItemTemplate&gt;
          &lt;EditItemTemplate&gt;
              &lt;asp:Label ID="lblUser" runat="server" Text='&lt;%# Eval("Incumbent")%&gt;' Visible = "false"&gt;&lt;/asp:Label&gt;            
              &lt;asp:DropDownList Width="100%" runat="server" 
                 id="ddlUsers" AutoPostBack="true" 
                 DataTextField="FullName" DataValueField="UserID"
                 OnSelectedIndexChanged="ddlUsers_SelectedIndexChanged"&gt;
              &lt;/asp:DropDownList&gt; 
          &lt;/EditItemTemplate&gt; 
      &lt;/asp:TemplateField&gt;
      &lt;asp:TemplateField HeaderText="Backup"&gt;
          &lt;ItemTemplate&gt;
              &lt;%#Eval("Backup")%&gt;
          &lt;/ItemTemplate&gt;
          &lt;EditItemTemplate&gt;
              &lt;asp:Label ID="lblBackup" runat="server" Text='&lt;%# Eval("Backup")%&gt;' Visible = "false"&gt;&lt;/asp:Label&gt;         
              &lt;asp:DropDownList Width="100%" runat="server" 
                 id="ddlUsersBackup" AutoPostBack="true" 
                 DataTextField="FullName" DataValueField="UserID"
                 OnSelectedIndexChanged="ddlUsersBackup_SelectedIndexChanged"&gt;
              &lt;/asp:DropDownList&gt; 
          &lt;/EditItemTemplate&gt;     
      &lt;/asp:TemplateField&gt;
  
      &lt;asp:CommandField ButtonType="Button" ControlStyle-CssClass="coolbutton" 
          ShowEditButton="true" 
          ShowDeleteButton="true"
          ShowCancelButton="true" /&gt; 
  </code></pre>
  
  <p>When I ignore the error, the Command buttons are indeed in the 5th column:</p>
  
  <p><img src="https://i.stack.imgur.com/LhKXR.png" alt="When I ignore the error, the Command buttons are indeed in the 5th column."></p>
  
  <p>The error I get without the Try/Catch is: 
  <img src="https://i.stack.imgur.com/uQghH.png" alt="Problem on DirectCast"></p>