ASP.NET:Controls.Count始终为零

时间:2010-08-06 17:16:28

标签: asp.net user-controls repeater

我试图触发“Controls.Clear”,但它永远不会起作用,因为Controls.Count始终为零。情况并非如此。我已粘贴下面的程序逻辑。

注释是由一个控件构建的 - Comments.ascx - 它会触发下面的OnInit事件:

protected override void OnInit(EventArgs e)
{
            _presenter = new CommentsPresenter();
            _presenter.Init(this, IsPostBack);
}

并且CommentsPresenter类的Init方法填充视图:

public void Init(IComments view, bool isPostBack)
        {
            _view = view;

            _view.ShowCommentBox(_webContext.CurrentUser != null);
        }

填充表示层视图后,CommentsPresenter.cs中的以下两个方法用于设计UI。你会看到“AddComment”调用了有问题的“ClearComment”方法:

public void LoadComments()
        {
            _view.LoadComments(_commentRepository.GetCommentsBySystemObject(_view.SystemObjectId,
                                                                             _view.SystemObjectRecordId));    
        }

        public void AddComment(string comment)
        {
            var c = new Comment
                        {
                            Body = comment,
                            CommentByAccountId = _webContext.CurrentUser.AccountId,
                            CommentByUserName = _webContext.CurrentUser.UserName,
                            CreateDate = DateTime.Now,
                            SystemObjectId = _view.SystemObjectId,
                            SystemObjectRecordId = _view.SystemObjectRecordId
                        };
            _commentRepository.SaveComment(c);
            _view.ClearComments();
            LoadComments();
        }

现在,回到Comments.ascx服务器控件,我们看到如何触发“AddComment”(以及ClearComments):

protected void Page_Load(object sender, EventArgs e)
{
    _presenter.LoadComments();
}

protected void BtnAddCommentClick(object sender, EventArgs e)
{
    _presenter.AddComment(commentMark.Text);
    commentMark.Text = "";
}

public void ClearComments()
{
    if (commentPosted.Controls.Count > 0)
    {
        //var ctls = commentPosted.Controls[0];
        //    ctls.Controls.Clear();
        commentPosted.Controls.Clear();
    }
}

public void LoadComments(List<Comment> comments)
{
    var sb = new StringBuilder();

    if(comments.Count > 0)
    {
        commentPosted.Controls.Add(new LiteralControl("<div id=\"CommentPosted\">"));

        foreach (Comment comment in comments)
        {
            sb.Append("<div class=\"commentPanel\" id=\"record-" + comment.CommentId + "\" align=\"left\">");
            sb.Append("<a href=\"" + WebContext.RootUrl + comment.CommentByUserName + "\tabindex=\"-1\">");
            sb.Append(GetProfileImage(comment.CommentByAccountId) + "</a>");
            sb.Append("<label class=\"postedComments\">" + comment.Body + "</label>");
            sb.Append("<br clear=\"all\">"); 
                sb.Append("<span style=\"margin-left: 43px; color: rgb(102, 102, 102); font-size: 11px;\">few seconds ago");
            sb.Append("</span>&nbsp;&nbsp;<a href=\"#\" id=\"CID-" + comment.CommentId + "\" class=\"c_delete\">Delete</a></div>");

            commentPosted.Controls.Add(new LiteralControl(sb.ToString()));
        }

        commentPosted.Controls.Add(new LiteralControl("</div>"));

    }
}

以下是我的Comments.ascx用户控件的HTML:

<asp:UpdatePanel runat="server">
    <ContentTemplate>
        <asp:Panel runat="server" ID="pnlComment">
            <asp:PlaceHolder ID="commentPosted" runat="server"></asp:PlaceHolder>
            <div class="commentBox" id="commentBox" style="" align="right">
                <a href="<%= WebContext.RootUrl %><%= WebContext.CurrentUser.UserName %> " tabindex="-1">
                <%= GetProfileImage(WebContext.CurrentUser.AccountId) %></a>
                <label id="record-128">
                    <asp:TextBox ID="commentMark" Width="300" CssClass="commentMark" TextMode="MultiLine" Columns="60" runat="server"></asp:TextBox>
                </label>
                <br clear="all">
                <br />
                <asp:Button Text="Comment" ID="btnAddComment" style="display:inline-block;" CssClass="small button comment" runat="server" OnClick="BtnAddCommentClick" />
            </div>
        </asp:Panel>
    </ContentTemplate>
</asp:UpdatePanel>

最后,下面是生成的HTML:

        <div id="ctl00_ContentCenter_repFilter_ctl08_Comments1_pnlComment">


            <div class="commentBox" id="commentBox" style="" align="right">
                <a href="http://localhost:1663/GrumpyCat%20" tabindex="-1">

                <img alt="" src="http://localhost:1663/images/ProfileAvatar/ProfileImage.aspx?AccountId=53&amp;w=30&amp;h=30" style="float: left;" width="30" height="30"></a>
                <label id="record-128">
                    <textarea name="ctl00$ContentCenter$repFilter$ctl08$Comments1$commentMark" rows="2" cols="60" id="ctl00_ContentCenter_repFilter_ctl08_Comments1_commentMark" class="commentMark" style="width: 300px; color: rgb(51, 51, 51);"></textarea>
                </label>
                <br clear="all">
                <br>
                <input name="ctl00$ContentCenter$repFilter$ctl08$Comments1$btnAddComment" value="Comment" id="ctl00_ContentCenter_repFilter_ctl08_Comments1_btnAddComment" class="small button comment" style="display: inline-block;" type="submit">
            </div>


        </div>

谢谢!

1 个答案:

答案 0 :(得分:2)

根据您何时致电Controls.Clear,可能尚未创建控件。查看ASP.NET页面生命周期:http://msdn.microsoft.com/en-us/library/ms178472.aspx

您似乎也在浏览器中发布HTML源代码 - 如果上述内容无法解决,请发布.aspx源代码并告诉我们您在何处访问Controls集合。

希望有所帮助。