2010-08-06 5 views
0

"Controls.Clear"를 실행하려고하는데 Controls.Count가 항상 0이기 때문에 절대로 작동하지 않습니다. 이것은 사실이 아니어야합니다. 아래 프로그램 논리를 붙여 넣었습니다. Comments.ascx - - ASP.NET : Controls.Count 항상 0입니다.

코멘트

는 제어에 의해 내장 된하는 OnInit 이벤트 아래 화재 : 일단

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

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

:

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

그리고 CommentsPresenter 클래스 초기화 방법과 같은보기를 채 웁니다 프레젠테이션 계층보기가 채워지면 CommentsPresenter.cs의 다음 두 가지 방법을 사용하여 UI를 디자인합니다. 해고 된 Comments.ascx 서버 컨트롤에 다시, 우리는 어떻게 "AddComment"(따라서 ClearComments)를 참조하십시오 이동 지금

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

: "AddComment"는 질문에 "ClearComment"방법을 호출되어 있음을 당신은 볼 수

마지막으로
<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은 다음과 같습니다 :

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이 모습입니다

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

고마워요!

답변

2

Controls.Clear에 전화를 걸 때에 따라 컨트롤이 아직 만들어지지 않을 수 있습니다. ASP.NET 페이지 수명주기 : http://msdn.microsoft.com/en-us/library/ms178472.aspx을 확인하십시오.

브라우저에서 HTML 소스를 게시하는 것 같습니다. 위의 방법으로 해결할 수없는 경우 .aspx 소스 코드를 게시하고 Controls 컬렉션에 액세스하고 있는지 알려주세요.

희망이 있습니다.

+0

감사합니다.하지만 당신이 제공 한 링크에서 읽은 것을 바탕으로, 올바른 순서로 된 것 같습니다. 내 코드를 게시했습니다. 어떤 것이 있는지 알아보십시오. –

+0

안녕하세요, 코드가 잘 보이지만 'ClearComments'가 어디에서 왔는지 표시하지 않습니다. 아마도 작동하지 않는 비트 일 것입니다. 어디에서 전화가 왔습니까? –

+0

다시 한번 감사드립니다. 나는 나의 질문에 개심을 기탁했다. - 희망을 갖고 나의 편집은 사물을 분명히 할 것이다? 알려줘 ... 다시 한 번 감사드립니다. –

관련 문제