CommandName =“Delete”不会调用任何方法

时间:2011-06-19 12:52:44

标签: c# asp.net asp.net-controls

我有一个看起来像这样的Repeater控制器:

<asp:Repeater ID="Postrepeater" runat="server" DataSourceID="datasource" >
<h2>
    <%#Eval("posttitle") %>
</h2>
<p>
    <%#Eval("posttext") %>
</p>
<asp:Button ID="DeleteLinkButton" runat="server" CommandName="Delete" Text="Delete post" />
</asp:Repeater>

数据源:

<asp:ObjectDataSource ID="datasource" TypeName="Service" SelectMethod="GetPosts" DataObjectTypeName="Post" runat="server" DeleteMethod="DeletePost" />

在Service.cs中

public List<Posts> GetPosts()
        {
            DALposts dal = new DALposts();
            return dal.GetPosts();
        }

public void DeletePost(Posts post)
    {
        DALPost.DeletePost(post.PostId);
    }

真正奇怪的是SelectMethod,GetPosts作为一种魅力 - 正如它应该的那样。但是当我尝试删除帖子时,DeleteMethod不会调用任何方法 - 页面只是重新加载而没有任何反应。我试图调试代码,但是CommandName =“Delete”根本没有调用任何东西,我没有收到任何错误......有什么想法吗?

认为我发布了足够的代码,但如果您认为需要更多代码 - 请告诉我。

更新 经过大量的阅读和一些帮助后,我得出的结论是,除非我写了很多代码,否则无法用转发器完成 - 所以我改为使用FormView。像魅力一样工作

2 个答案:

答案 0 :(得分:1)

要通过设置CommandName来完成某项工作,您必须实施OnItemCommand check this

答案 1 :(得分:1)

我不认为你用转发器描述它的方式是可能的。你必须使用gridview。

或者你可以在转发器上使用OnItemCommand事件,检查Delete参数然后调用你的删除方法。