Hi There. Thanks for reading.
I have a custom control, comments.ascx. In that page, I have the following methods:
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 = "";
}
And here is the guts of the CommentsPresenter Class:
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();
}
I also have a page Updates.aspx (that references the Comments user control). In that page, I have the following:
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);
}
THE PROBLEM I AM HAVING is that when I add if (!IsPostBack) to the above Page_Load event and update my status, then all the comments on the page get cleared. But, when I remove if (!IsPostBack), then the comments update when I update my status but the submit button in my Comments user control won't fire!
I am not dynamically adding my custom control so I don't think it is a precedence issue. I can't figure this out. Any idea what is happening?
Thanks for your help / suggestions / advice...