Choosing a Row to Delete

This demo illustrates how to use the DataGrid's ItemCommand event in order to affix a client-side onclick event handler to the Delete button. Note that even if you opt to delete a record, no records are actually deleted - I don't want anyone to be able to destroy the database, after all! :-)

 FAQ IDQuestion
Delete2How do you convert a string to all uppercase or all lowercase?
Delete12How can I display line breaks in an email message?
Delete14Can I send emails without using CDONTS?
Delete15How can I determine the length of a string (how many characters are in a string)?
Delete17How can I find the position of a certain character (or substring) within a string?
Delete19How can I return the current date/time?
Delete20What is recursion?
Delete23What, exactly, is the FileSystemObject?
Delete25How can I send an email using CDONTS?
Delete8How do you create a database table through a SQL statement (via an ASP page, perhaps)?
Delete16Using VBScript, how can I determine if a variable is a valid date or not?
Delete18How can I easily display a nicely formatted date variable?
Delete24How can I read the contents of a text file through an ASP page?
Delete7How do I dynamically resize an array?
Delete11How can I have my ASP page send HTML-formatted email?
Delete10How can I tell what version of ADO I am using?
Delete13How do you send email attachments through an ASP page?
Delete21How can I determine the upper or lower bounds of an array?
Delete22How can I validate an email adress using Jscript?

Source Code
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">
  'Create a connection
  Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
  Dim ddlDataSet as DataSet = New DataSet()

  Sub Page_Load(sender as Object, e as EventArgs)
    If Not Page.IsPostBack then
    End If
  End Sub
  Function GetCategories() as DataSet
    'Populate the ddlDataSet
    Const strSQLDDL = "SELECT FAQCategoryID, Name FROM tblFAQCategory ORDER BY Name"    
    Dim myDataAdapter as SqlDataAdapter = New SqlDataAdapter(strSQLDDL, myConnection)    
    myDataAdapter.Fill(ddlDataSet, "Categories")

    Return ddlDataSet
  End Function

  Sub BindData()
    '2. Create the command object, passing in the SQL string
    Const strSQL as String = "SELECT FAQID, F.Description FROM tblFAQ F WHERE FAQID <= 25"

    'Set the datagrid's datasource to the datareader and databind
    Dim resultsDataSet as New DataSet()
    Dim myDataAdapter as SqlDataAdapter = New SqlDataAdapter(strSQL, myConnection) 

    dgPopularFAQs.DataSource = resultsDataSet
  End Sub

  Sub dgPopularFAQs_Delete(sender As Object, e As DataGridCommandEventArgs)
    lblResults.Text = "Opted to delete DataGrid row " & e.Item.ItemIndex & _
                      ", which has a FAQID of " & dgPopularFAQs.DataKeys(e.Item.ItemIndex)

    'Here you would insert the code to make the database delete call
    'When done, you need to rebind the DataGrid so that the deleted FAQ is no longer
  End Sub
  Sub dgPopularFAQs_ItemDataBound(sender as Object, e as DataGridItemEventArgs)
    ' First, make sure we're NOT dealing with a Header or Footer row
    If e.Item.ItemType <> ListItemType.Header AND e.Item.ItemType <> ListItemType.Footer then
      'Now, reference the LinkButton control that the Delete ButtonColumn has been rendered to
      Dim deleteButton as LinkButton = e.Item.Cells(0).Controls(0)

      'We can now add the onclick event handler
      deleteButton.Attributes("onclick") = "javascript:return confirm('Are you sure you want to delete FAQ #" & _
                    DataBinder.Eval(e.Item.DataItem, "FAQID") & "?')"    
    End If
  End Sub

  <form runat="server">
    <asp:label id="lblResults" Font-Bold="True" runat="server" 
		Font-Italic="True" ForeColor="#aa0000" />
    <asp:datagrid id="dgPopularFAQs" runat="server"


		<asp:ButtonColumn Text="Delete" CommandName="Delete" />
		<asp:BoundColumn DataField="FAQID" HeaderText="FAQ ID"
		     ItemStyle-HorizontalAlign="Center" />
		<asp:BoundColumn DataField="Description" HeaderText="Question" />

[Return to the article]