我的删除参数有问题。我正在使用GridView和ObjectDataSource。 我想删除一行。但是当我调试时,我的业务逻辑层中的CustomerId值始终为0.
这是我的代码
我有两个删除参数,Id
和CustomerId
。这些名称与我BLL中的名称相同。
<asp:GridView DataKeyNames="Id" ID="gvFavoriteMovies" DataSourceID="odsFavoriteMovies" AutoGenerateColumns="False"
runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblTitel" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Title")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="odsFavoriteFilm" runat="server"
TypeName="MovieMonstrDataLayer.bll.BLLMovies"
SelectMethod="GetFavoriteMoviesFromUser"
DeleteMethod="DeleteFavoriteMoviesFromUser"
onobjectcreating="odsFavoriteFilm_ObjectCreating"
ondeleting="odsFavoriteFilm_Deleting">
<DeleteParameters>
<asp:Parameter Name="Id" DbType="Int32" Direction="Input" />
<asp:Parameter Name="CustomerId" DbType="Int32" Direction="Input" />
</DeleteParameters>
<SelectParameters>
<asp:Parameter DbType="Int32" Direction="Input" Name="CustomerId" />
</SelectParameters>
</asp:ObjectDataSource>
这是在文件背后的代码
删除行时,我将登录客户的值分配给删除参数CustomerId
。这一切都有效。我给delete参数赋予了正确的值。
protected void odsFavoriteFilm_Deleting(object sender, ObjectDataSourceMethodEventArgs e)
{
if (Context.User.Identity.IsAuthenticated)
{
this.odsFavoriteFilm.DeleteParameters["CustomerId"].DefaultValue = ((MovieMonstrIdentity)Context.User.Identity).Customer.Id.ToString();
this.odsFavoriteFilm.DataBind();
}
}
通常在我的业务逻辑层,我现在应该得到正确的CustomerId
,但我总是得到0.而没有Costumer有这个ID。
public int DeleteFavoriteFilmFromUser(int Id, int CustomerId)
{
try
{
return Adapter.DeleteFavoriteFilmFromUser(Id, CustomerId);
}
catch (Exception ex)
{
throw ex;
}
}
我做错了什么?
答案 0 :(得分:2)
在CustomerID
DataKeyNames
<asp:GridView DataKeyNames="Id,CustomerId" ...>
在更新之前执行此操作
protected odsFavoriteFilm_DataBinding(object sender, EventArgs e) {
if(!IsPostBack) {
this.odsFavoriteFilm.DeleteParameters["CustomerId"].DefaultValue = ((MovieMonstrIdentity)Context.User.Identity).Customer.Id.ToString();
}
}
或设置命令的实际参数值
protected void odsFavoriteFilm_Deleting(object sender, ObjectDataSourceMethodEventArgs e) {
if (Context.User.Identity.IsAuthenticated) {
e.Command.Parameters["CustomerId"].Value = ((MovieMonstrIdentity)Context.User.Identity).Customer.Id.ToString();
}
}