谢谢阅读。
我有一个自定义控件,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(它引用了 Comments 用户控件)。在该页面中,我有以下内容:
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);
}
我遇到的问题是,当我将if (!IsPostBack)添加到上述 Page_Load 事件并更新我的状态时,页面上的所有评论都会被清除。但是,当我删除if (!IsPostBack)时,评论会在我更新状态时更新,但我的评论用户控件中的提交按钮不会触发!
我没有动态添加我的自定义控件,所以我认为这不是优先问题。我想不通。知道发生了什么吗?
感谢您的帮助/建议/建议...