在允许插入、编辑和删除的 ASP.Net/VB.Net DetailsView 上,我们希望捕获当用户单击详细信息视图的"编辑"按钮和"更新"按钮时发生的事件。
我们希望在 VB.Net 代码隐藏文件中捕获这些事件和事件处理程序。
你能用示例代码告诉我怎么做吗?
*更新*
我尝试了此编码,但在单击"编辑"按钮时出现以下错误:
Unable to cast object of type 'System.Web.UI.WebControls.DetailsView'
to type 'System.Web.UI.WebControls.DetailsViewRow'.
这是显示"编辑"按钮的标记中的编码:
<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>
这是代码隐藏文件中的处理程序:
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
我们在 Dim 语句上得到错误。
您可以使用适当的CommandName
使用它ItemCommand
事件:
<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>
在代码隐藏中:
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
定义一个函数来处理事件。
事件列在这里:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview_events.aspx
然后,您将编写一个处理程序来捕获更新事件(在更新发生之前):
Protected Sub detailsView1_ItemUpdating(sender As Object, e As DetailsViewUpdateEventArgs) Handles detailsView1.ItemUpdating
'Code here
End Sub
要进行编辑,请捕获模式更改事件:
Protected Sub detailsView1_ModeChanging(sender As Object, e As DetailsViewUpdateEventArgs) Handles detailsView1.ModeChanging
'You then check the new edit mode
If e.NewMode = DetailsViewMode.Edit Then
'Code here
End If
End Sub
还可以在 DetailsView 控件的标记中添加处理程序:
<asp:DetailsView runat="server" ID="detailsView1" OnItemUpdating="detailsView1_ItemUpdating" OnModeChanging="detailsView1_ModeChanging">
....
</asp:DetailsView>
这意味着您不再需要将Handles detailsView1.ItemUpdating
或Handles detailsView1.ModeChanging
放在函数的末尾。