views:

38

answers:

1

Hi.

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 presentation 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;amp;w=30&amp;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!

+2  A: 

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.

Kieren Johnstone
Thanks Kieren but based on what I have read in the link you have provided, things seem to be in the right order. I have posted my code, let me know if anything stands out?
Code Sherpa
Hi, the code looks fine, but you're not showing where `ClearComments` is called from - presumably that's the bit that's not working. Where is it called from?
Kieren Johnstone
Thanks again. I have given my question a facelift - hopefully my edits will clarify things? Let me know... thanks again.
Code Sherpa
No, I'm completely lost to be honest. Not used to that coding style. Would it help at all to say that controls aren't persisted automatically: if you create them programmatically, you must do so on every single request. You might be doing that in `ShowCommentBox`, but the source isn't there. Sorry it's a bit vague!
Kieren Johnstone