【问题标题】:Trapping Edit & Update events from DetailsView button clicks从 DetailsView 按钮单击中捕获编辑和更新事件
【发布时间】:2012-11-14 14:28:06
【问题描述】:

在允许插入、编辑和删除的 ASP.Net / VB.Net DetailsView 上,我们希望捕获当用户单击 DetailsView 的 Edit 按钮和 Update 按钮时发生的事件。

我们希望通过 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"  />
                &nbsp;<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" />
                &nbsp;<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" />
                &nbsp;<asp:Button ID="ButtonNew" runat="server" CausesValidation="False" 
                    CommandName="New" Text="New" />
                &nbsp;<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 语句中得到错误。

【问题讨论】:

    标签: asp.net vb.net event-handling onclick code-behind


    【解决方案1】:

    您可以将其ItemCommand 事件与适当的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>
    

    在代码隐藏中:

    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
    

    【讨论】:

    • 感谢大家的快速回复。 :-) 我会尝试这两种方法。我希望我可以选择您的两个回复作为接受的答案。 :-( 也许 Stackoverflow 将来会允许这样做。:-)
    • 噢,来吧,他有很多代表='[开玩笑,很高兴帮助=]
    • 代码隐藏文件中的 Dim 语句出现错误。你能检查一下我的编码,看看我们遗漏了什么吗?
    • @Emad-ud-deen:抱歉,将 DetailsView 与 ListView(GridView 等)混合在一起。所以使用DetailsView.Rows 属性来获取所有行。
    • 您已经找到了发送者,它将成为按钮。将sender 转换为按钮,然后使用其父级获取标签。类似Dim LabelRegistrationFee As Label = CType(CType(sender, Control).Parent.FindControl("LabelRegistrationFee"), Label)
    【解决方案2】:

    你定义一个函数来处理这个事件。

    这里列出了这些事件: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
    

    为了编辑你捕获 ModeChanging 事件:

    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.ItemUpdatingHandles detailsView1.ModeChanging 放在函数的末尾。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-07
      • 1970-01-01
      • 2021-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多