问题描述
|
我试图将参数添加到ObjectDataSource的删除事件中,如以下示例中的msdn。我为ObjectDataSource的deleteing事件生成了一个事件处理程序,它具有与示例中相同的签名,但是,当我尝试清除示例中的paramsFromPage时,收到一条错误消息,指出paramsFromPage是只读的。我需要在其他地方进行更改吗?
此示例来自以下msdn页面:
http://msdn.microsoft.com/zh-cn/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是只读的,无法修改。”。
解决方法
对于Microsoft建议的解决方案,我找到了一个很好的解决方法。我发现,虽然不能以编程方式添加或删除参数,但可以声明性地添加它们,并且delete函数所需的参数会自动添加。同样,可以在删除事件中以编程方式修改集合中的参数。
这是我的解决方案:
\' 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
我只是想知道是否有更好的方法从单元格中获取数据,以便更改列顺序不会破坏此代码。