0

谢谢阅读。

我有一个自定义控件,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)时,评论会在我更新状态时更新,但我的评论用户控件中的提交按钮不会触发!

我没有动态添加我的自定义控件,所以我认为这不是优先问题。我想不通。知道发生了什么吗?

感谢您的帮助/建议/建议...

4

1 回答 1

0

您是否需要在每次往返时都进行数据绑定,因为您的控件不保存视图状态,但您的提交却是因为它是 ImageButton 或 LinkBut​​ton?

我厌倦了阅读所有这些(对不起,注意力集中时间短)。

这是我认为可能发生的事情:

1)您需要在每次往返时进行 DataBind,因为您的控件不保存视图状态,因此您不能使用if (!IsPostBack),因为它不会在回发时绑定,因此在页面返回时不显示任何数据。

2)因为您在页面加载时一直在连接提交按钮,所以它不会被触发,因为您实际上一直在重置页面。

当然我可能都错了...

于 2010-08-18T02:26:51.097 回答