开发者

ASP.NET: Controls.Count always zero

I am trying to fire "Controls.Clear" but it never works because Controls.Count is always zero. This shouldn't be the case. I have pasted the program logic below.

The comments are built by a control - Comments.ascx - which fires the below OnInit event:

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

And the CommentsPresenter class Init method populates the view as such:

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

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

Once the prese开发者_如何转开发ntation layer view is populated, the following two methods within CommentsPresenter.cs are used to design the UI. You will see that "AddComment" calles the "ClearComment" method in question:

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();
        }

Now, moving back to the Comments.ascx server control, we see how "AddComment" (and therefore ClearComments) is fired:

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>"));

    }
}

AND here is what the HTML for my Comments.ascx User Control looks like:

<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>

Finally, below is the Generated 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>

Thanks!


Depending on when you are calling Controls.Clear, the controls might not be created yet. Check out the ASP.NET page lifecycle: http://msdn.microsoft.com/en-us/library/ms178472.aspx.

It looks like you're posting HTML source from a browser too - if the above doesn't solve it for you, please post your .aspx source code and tell us where you are accessing the Controls collection.

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜