我遇到了一个棘手的问题,一个ObjectDataSource(绑定到DetailsView)没有检查DeleteParameters的delete方法(我在标记和数据对象中都指定了需要整数)。 这是标记:
<asp:ObjectDataSource ID="odsFrontPageBannerDetails" runat="server" DataObjectTypeName="FrontPage.Banner" DeleteMethod="DeleteFrontPageBanner" InsertMethod="InsertFrontPageBanner" SelectMethod="GetFrontPageBanner" UpdateMethod="UpdateFrontPageBanner" TypeName="FrontPage.BannersData">
<DeleteParameters>
<asp:Parameter Name="idx" Type="Int32" />
</DeleteParameters>
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="idx" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
和对象:
<DataObjectMethod(DataObjectMethodType.Delete, True)> _
Public Shared Function DeleteFrontPageBanner(ByVal idx As Integer) As Integer
Dim retval As Integer = 0
Dim iReturnCode As Integer = 0
Using conn As New SqlConnection(GetConnStr())
Using cmd As New SqlCommand
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "FrontPage_BannerDelete"
cmd.Connection = conn
cmd.Parameters.AddWithValue("@idx", idx)
Try
conn.Open()
iReturnCode = cmd.ExecuteNonQuery
If iReturnCode >= 0 Then
retval = 1
LastErrorMsg = String.Empty
Else
Throw New DataException("Return code is: " & iReturnCode)
End If
Catch ex As Exception
LastErrorMsg = ex.Message
retval = 0
End Try
End Using
End Using
Return retval
End Function
和detailsview标记:
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" CellPadding="4" DataSourceID="odsFrontPageBannerDetails" ForeColor="#333333" GridLines="None" Height="50px" Width="338px">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
<EditRowStyle BackColor="#999999" />
<FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
<Fields>
<asp:BoundField DataField="idx" HeaderText="idx" ReadOnly="True" SortExpression="idx" />
<asp:CheckBoxField DataField="Active" HeaderText="Active" SortExpression="Active" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="BannerText" HeaderText="BannerText" SortExpression="BannerText" />
<asp:BoundField DataField="BannerImage" HeaderText="Banner Image" SortExpression="BannerImage" />
<asp:BoundField DataField="BannerLink" HeaderText="Go To Page Link" SortExpression="BannerLink" />
<asp:BoundField DataField="BackgroundColor" HeaderText="Background Color" SortExpression="BackgroundColor" />
<asp:BoundField DataField="BannerHeight" HeaderText="Banner Height" HtmlEncode="False" SortExpression="BannerHeight" />
<asp:BoundField DataField="StartDate" DataFormatString="{0:MM/dd/yyyy}" HeaderText="Start Date" HtmlEncode="False" SortExpression="StartDate" />
<asp:BoundField DataField="EndDate" DataFormatString="{0:MM/dd/yyyy}" HeaderText="End Date" HtmlEncode="False" SortExpression="EndDate" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
</Fields>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
</asp:DetailsView>
错误表示DeleteParameters完全被忽略! (我直接从视频教程中看到了一个例子,它对作者来说效果很好)
ObjectDataSource'odsFrontPageBannerDetails'找不到 带有参数的非泛型方法'DeleteFrontPageBanner' 输入'FrontPage.Banner'。
有什么想法吗?我试过重建解决方案和页面没有运气!
在尝试了我可以想到的每个参数组合没有成功之后,我决定只更改delete方法以接受Frontpage.Banner对象,现在它删除没有错误 - 请参阅下面的更改:
<DataObjectMethod(DataObjectMethodType.Delete, True)> _
Public Shared Function DeleteFrontPageBanner(ByVal o As FrontPage.Banner) As Integer
Dim retval As Integer = 0
Dim iReturnCode As Integer = 0
Using conn As New SqlConnection(GetConnStr())
Using cmd As New SqlCommand
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "FrontPage_BannerDelete"
cmd.Connection = conn
cmd.Parameters.AddWithValue("@idx", o.idx)
Try
conn.Open()
iReturnCode = cmd.ExecuteNonQuery
If iReturnCode >= 0 Then
retval = 1
LastErrorMsg = String.Empty
Else
Throw New DataException("Return code is: " & iReturnCode)
End If
Catch ex As Exception
LastErrorMsg = ex.Message
retval = 0
End Try
End Using
End Using
Return retval
End Function
仍然希望解释为什么标记中的删除参数被完全忽略。
答案 0 :(得分:0)
通常,对象数据源将根据参数名称以任何顺序解析方法,而不检查数据类型。
但是,如果设置了DataObjectTypeName属性,则以不同的方式解析该方法。 ObjectDataSource查找具有在DeleteMethod属性中指定的名称的方法,该属性采用DataObjectTypeName属性中指定的类型的一个参数。在这种情况下,参数的名称无关紧要。
请参阅here