views:

34

answers:

1

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

A: 

Do you need to databind at every round-trip because your controls don't save viewstate but your submit does because it's an ImageButton or LinkButton?

I got bored reading all that (sorry short concentration span).

Here's what i think could be happening:

1) You need to DataBind at every round-trip because your control doesn't save viewstate therefore you can't use if (!IsPostBack) because it doesn't bind on postback therefore not showing any data when the page comes back.

2) Because you're hooking up your submit button all the time at page load it doesn't get triggered because you're actually resetting your page all the time.

Ofcourse i could be all wrong...

Jeroen
Thanks Jeroen, I appreciate that. I got side-tracked on another issue but this one still exists. I'll return to it tomorrow and follow up. Thanks a bundle!
Code Sherpa