修改ObjectDataSource删除参数时出现问题

发布于 2024-11-10 08:06:50 字数 1904 浏览 3 评论 0原文

我正在尝试向 ObjectDataSource 的删除事件添加一个参数,如下面来自 msdn 的示例所示。我为 ObjectDataSource 的删除事件生成了一个事件处理程序,并且它具有与示例中相同的签名,但是,当我尝试像示例中那样清除 paramsFromPage 时,我收到一条错误,指出 paramsFromPage 是只读的。我需要在其他地方改变什么吗?

此示例来自以下 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 是只读的,无法修改。”当尝试添加到有序字典时抛出该错误。

I am trying to add a parameter to the deleting event of an ObjectDataSource as in the example below from msdn. I generated an event handler for the ObjectDataSource's deleting event, and it had the same signature as in the example, however, when I try to clear the paramsFromPage as in the example, I receive an error stating that paramsFromPage is readonly. Is there something I need to change somewhere else?

This example comes from the following msdn page:

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

EDIT:
The following is my code

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

The error "The OrderedDictionary is readonly and cannot be modified." is thrown when an attempt is made to Add to the ordered dictionary.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往事风中埋 2024-11-17 08:06:50

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

这是我的解决方案:

' 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

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

I found a good workaround to the solution Microsoft suggested. I discovered that while parameters cannot be added or removed programmatically, they can be added declaratively, and the parameters needed for the delete function are added automatically. Also, the parameters that are in the collection can be modified programatically inside the deleting event.

Here is my solution:

' 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

I am just wondering if there is a better way to get the data from the cells so that changing the column order does not break this code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文