読んでくれてありがとう。
カスタムコントロールcomments.ascxがあります。そのページには、次の方法があります。
protected override void OnInit(EventArgs e)
{
_presenter = new CommentsPresenter();
_presenter.Init(this, IsPostBack);
}
public Comments()
{
WebContext = ObjectFactory.GetInstance<IWebContext>();
}
protected void Page_Load(object sender, EventArgs e)
{
if (commentPosted.Controls.Count > 0)
commentPosted.Controls.Clear();
_presenter.LoadComments();
}
protected void BtnAddCommentClick(object sender, EventArgs e)
{
_presenter.AddComment(commentMark.Text);
commentMark.Text = "";
}
そして、これがCommentsPresenterクラスの根性です:
private IComments _view;
private readonly ICommentRepository _commentRepository;
private readonly IWebContext _webContext;
public CommentsPresenter()
{
_commentRepository = ObjectFactory.GetInstance<ICommentRepository>();
_webContext = ObjectFactory.GetInstance<IWebContext>();
}
public void Init(IComments view, bool isPostBack)
{
_view = view;
_view.ShowCommentBox(_webContext.CurrentUser != null);
}
public void LoadComments()
{
_view.LoadComments(_commentRepository.GetCommentsBySystemObject(_view.SystemObjectId,
_view.SystemObjectRecordId));
}
public void AddComment(string comment)
{
if (_webContext != null)
{
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();
}
Updates.aspx(コメントユーザーコントロールを参照する)ページもあります。そのページには、次のものがあります。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (_webContext.AccountId > 0)
_presenter.Init(this, _webContext.AccountId);
else if (_userSession.CurrentUser != null)
_presenter.Init(this, _userSession.CurrentUser.AccountId);
}
}
protected void BtnAddStatusClick(object sender, EventArgs e)
{
var id = default(int);
if (_webContext.AccountId > 0)
id = _webContext.AccountId;
else if (_userSession.CurrentUser != null)
id = _userSession.CurrentUser.AccountId;
var su = new StatusUpdate
{
CreateDate = DateTime.Now,
AccountId = id,
Status = updateText.Text
};
_statusRepository.SaveStatusUpdate(su);
_alertService.AddStatusUpdateAlert(su);
_presenter.Init(this, id);
}
私が抱えている問題は、上記のPage_Loadイベントにif(!IsPostBack)を追加してステータスを更新すると、ページ上のすべてのコメントがクリアされることです。ただし、if(!IsPostBack)を削除すると、ステータスを更新するとコメントが更新されますが、コメントユーザーコントロールの送信ボタンが起動しません。
カスタムコントロールを動的に追加していないので、優先順位の問題ではないと思います。私はこれを理解することはできません。何が起こっているのか分かりますか?
あなたの助け/提案/アドバイスをありがとう...