问题修改ObjectDataSource删除参数

时间:2011-05-26 14:36:17

标签: .net vb.net data-binding objectdatasource

我正在尝试将一个参数添加到ObjectDataSource的delete事件中,如下面的例子所示。我为ObjectDataSource的删除事件生成了一个事件处理程序,并且它具有与示例中相同的签名,但是,当我尝试清除paramsFromPage时,如示例中所示,我收到一条错误,指出paramsFromPage是readonly。我需要在其他地方改变一些东西吗?

此示例来自以下msdn页面:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.deleting%28v=VS.90%29.aspx

Private Sub NorthwindEmployeeDeleting(ByVal source As Object, ByVal e As ObjectDataSourceMethodEventArgs)

    ' The GridView passes the ID of the employee
    ' to be deleted. However, the business object, EmployeeLogic,
    ' requires a NorthwindEmployee parameter, named "ne". Create
    ' it now and add it to the parameters collection.
    Dim paramsFromPage As IDictionary = e.InputParameters

    If Not paramsFromPage("EmpID") Is Nothing Then

        Dim ne As New NorthwindEmployee(paramsFromPage("EmpID").ToString())
        ' Remove the old EmpID parameter.
        paramsFromPage.Clear()
        paramsFromPage.Add("ne", ne)


    End If
End Sub ' NorthwindEmployeeDeleting

编辑: 以下是我的代码

Protected Sub QueueDataSource_Deleting(ByVal sender As Object, ByVal e As ObjectDataSourceMethodEventArgs) Handles QueueDataSource.Deleting
    Dim paramsFromPage As IDictionary = e.InputParameters
    Dim queue As New QueueData

    If Not paramsFromPage("QueueNamek__BackingField") Is Nothing Then
        queue.QueueNamek__BackingField = paramsFromPage("QueueNamek__BackingField")
    End If

    If Not paramsFromPage("ServerNamek__BackingField") Is Nothing Then
        queue.ServerNamek__BackingField = paramsFromPage("ServerNamek__BackingField")
    End If

    paramsFromPage.Add("queue", queue)
End Sub

错误“OrderedDictionary是只读的,无法修改。”尝试添加到有序字典时抛出。

1 个答案:

答案 0 :(得分:0)

我找到了微软建议的解决方案。我发现虽然无法以编程方式添加或删除参数,但可以以声明方式添加它们,并自动添加删除功能所需的参数。此外,可以在删除事件中以编程方式修改集合中的参数。

这是我的解决方案:

' Store the index of the row being deleted
Protected Sub QueueGrid_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles QueueGrid.RowDeleting
    rowChosen = e.RowIndex
End Sub

' Prepare parameter to be sent to deleting function
Protected Sub QueueDataSource_Deleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles QueueDataSource.Deleting
    Dim queue As QueueData = e.InputParameters("queue")

    With queue
        .QueueNamek__BackingField = QueueGrid.Rows(rowChosen).Cells(2).Text
        .ServerNamek__BackingField = QueueGrid.Rows(rowChosen).Cells(3).Text
    End With
End Sub

我只是想知道是否有更好的方法从单元格中获取数据,以便更改列顺序不会破坏此代码。