父级 Page_Load 中的 If(!IsPostBack) 会导致自定义控件的单击事件中断。删除它可以解决问题,但会阻止内容加载!

发布于 2024-09-14 22:47:02 字数 3811 浏览 9 评论 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) 时,评论会在我更新状态时更新,但评论用户控件中的提交按钮不会触发!

我没有动态添加自定义控件,因此我认为这不是优先级问题。 我无法弄清楚这一点。知道发生了什么吗?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

活泼老夫 2024-09-21 22:47:02

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

我厌倦了阅读所有这些(抱歉注意力集中时间短)。

我认为可能会发生以下情况:

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

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

当然我可能全错了......

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文