On an ASP.Net / VB.Net DetailsView that allows inserts, edits, and deletes we would like to trap the event that occurs when a user clicks on the Edit button and the Update button of the DetailsView.
We are looking to trap these events with and event handler inside a VB.Net code-behind file.
Can you tell me with sample code how to do it?
* Update *
I tried this coding but get the following error when clicking on the Edit button:
Unable to cast object of type 'System.Web.UI.WebControls.DetailsView'
to type 'System.Web.UI.WebControls.DetailsViewRow'.
This is the coding from the markup showing the Edit button:
<asp:DetailsView
ID="DetailsViewDetails"
runat="server"
AutoGenerateRows="False"
Height="50px"
Width="268px"
DataSourceID="SqlDataSourceDetails"
DataKeyNames="ID"
OnItemCommand="DetailsViewDetails_ItemCommand">
<Fields>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:Button ID="ButtonUpdate" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
<asp:Button ID="ButtonCancelUpdate" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
<asp:Button ID="ButtonInsert" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
<asp:Button ID="ButtonCancelInsert" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
<asp:Button ID="ButtonEdit" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit" />
<asp:Button ID="ButtonNew" runat="server" CausesValidation="False"
CommandName="New" Text="New" />
<asp:Button ID="ButtonDelete" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete" />
<AjaxToolKit:ConfirmButtonExtender ID="deleteButtonConfirmation"
runat="server"
ConfirmText='<%# "You are about to remove: " & vbcr &
Eval("Forename") & vbcr & Eval("Surname") & "!!!" &
vbcrlf & "Are you sure you want to do this?" & vbcrlf &
"Clicking the OK button will delete this parent." %>'
Enabled="True"
TargetControlID="ButtonDelete">
</AjaxToolKit:ConfirmButtonExtender>
</ItemTemplate>
</asp:TemplateField>
This is the handler in the code-behind file:
Protected Sub DetailsViewDetails_ItemCommand(sender As Object, e As System.Web.UI.WebControls.DetailsViewCommandEventArgs)
Dim row As DetailsViewRow = DirectCast(DirectCast(e.CommandSource, Control).NamingContainer, DetailsViewRow)
Select Case e.CommandName
Case "Add"
Case "Edit"
' Do this when going into edit mode so changes to the panent's tuition total balance can be updated.
'---------------------------------------------------------------------------------------------------
dcmOriginalRegistrationFee = GetValueFromTextBoxRegistrationFee()
Case "Delete"
End Select
End Sub
We get the error on the Dim statement.
You can use it's ItemCommand
event with an appropriate CommandName
:
<asp:DetailsView ID="DetailsView1" runat="server"
OnItemCommand="DetailsView1_ItemCommand"
<Fields>
<asp:BoundField DataField="IdField" HeaderText="ID" />
<asp:BoundField DataField="NameField" HeaderText="Name" />
<asp:ButtonField CommandName="Add" Text="Add Something" />
<asp:ButtonField CommandName="Edit" Text="EditSomething" />
<asp:ButtonField CommandName="Delete" Text="Delete Something" />
</Fields>
</asp:DetailsView>
in codebehind:
Protected Sub DetailsView1_ItemCommand(sender As Object, e As System.Web.UI.WebControls.DetailsViewCommandEventArgs)
Select Case e.CommandName
Case "Add"
Case "Edit"
Case "Delete"
End Select
End Sub