asp:在asp上没有访问Repeater ItemCommand事件:按钮单击asp.net Webforms

时间:2017-01-19 16:25:30

标签: c# asp.net webforms

我正在忙于ASP.Net Webforms应用程序,它要求我在转发器中放置一个按钮。该按钮应该删除我从数据库中获取的注释,尽管永远不会访问转发器的ItemCommand事件(我在事件方法中放置了一个断点来确定这一点。似乎有很多关于这个主题的帖子我尝试了许多建议,但似乎无法做到正确。我使用布尔变量" isAdmin"来确定登录用户是否具有管理权限然后显示2个中继器中的1个(" commentRepeater" /" commentRepeater2")。请找到.aspx内容页面中包含的以下代码:

 if (!IsPostBack)
        {
            DataBindRepeaters(blog.BlogID);
        }

        if (isAdmin)
        {   %>
    <asp:Repeater runat="server" ID="commentRepeater" 
        OnItemCommand="commentRepeater_ItemCommand"  >
        <ItemTemplate>
            <p><%# Eval("Comment") %></p>
            <p>User:<i><%# Eval("Username") %></i></p>
            <asp:Button runat="server" ID="btnDeleteComment" 
                CssClass="btnDeleteComment" Text="Delete Comment" 
                CommandName="btnDelete"  CommandArgument='<%# Eval("CommentId") %>' 
                         UseSubmitBehavior="false" CausesValidation="false" />
        </ItemTemplate>
    </asp:Repeater>
    <%  }

        else if (!isAdmin)
        {  %>
     <asp:Repeater runat="server" ID="commentRepeater2">
        <ItemTemplate>
            <p><%# Eval("Comment") %></p>
            <p>User:<i><%# Eval("Username") %></i></p>
        </ItemTemplate>
    </asp:Repeater>
      <% }

代码隐藏方法:

    protected void Page_Init(object sender,EventArgs e)
    {
        commentRepeater.ItemCommand += 
    new RepeaterCommandEventHandler(commentRepeater_ItemCommand);
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }

protected void commentRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "btnDelete")
        {
            int blogID = 0;
            int commentIdArgument;
            if (Int32.TryParse(e.CommandArgument.ToString(), out commentIdArgument))
            {
                using (ApplicationDbContext dbContext = new ApplicationDbContext())
                {
                    var comment = dbContext.Comments
                        .Where(c => c.CommentID == commentIdArgument)
                        .FirstOrDefault<Comment>();

                    if (comment != null)
                    {
                        blogID = comment.BlogId;
                        dbContext.Entry(comment).State = EntityState.Deleted;
                        dbContext.SaveChanges();
                    }

                }
            }
            if (blogID != 0)
            {
                Response.Redirect("/BlogPage.aspx?blogId=" + blogID);
            }
            else
            {
                Response.Redirect("/Default.aspx");
            }
        }
    }
    protected void DataBindRepeaters(int blogID)
    {
        var manager= Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
        DataTable table = new DataTable();
        table.Columns.Add("Comment", typeof(string));
        table.Columns.Add("Username", typeof(string));
        table.Columns.Add("CommentId", typeof(int));
        using (ApplicationDbContext dbContext = new ApplicationDbContext())
        {
            var blog = dbContext.Blogs.Where(b => b.BlogID == blogID)
            .FirstOrDefault<Blog>();
            string userName = String.Empty;
            foreach (var comment in blog.Comments)
            {
                userName = manager.FindById(comment.UserId).UserName;
                table.Rows.Add(comment.CommentContent, userName, comment.CommentID);
            }
        }
        commentRepeater.DataSource = table;
        commentRepeater.DataBind();
        commentRepeater2.DataSource = table;
        commentRepeater2.DataBind();
    }

因此,我尝试通过在代码隐藏内容页面的顶部添加Page_Init事件来注册转发器的ItemCommand事件方法,但没有成功。您的建议将不胜感激。

0 个答案:

没有答案