当 ASCX 子控件需要触发更改时,更新 ASPX 页面的最佳方法是什么?
当我有一个需要影响父页面中某些内容的子 .ASCX 控件时,我不完全确定在需要更新父页面中某些内容的情况下应该如何处理事件流。
我最终所做的就是将逻辑放入“Pre_Render”事件处理程序中。
由于这是在处理任何子 .ascx 控件之后进行处理的,因此我可以确保在显示 ASPX 页面之前正确呈现它。 我只是认为这不是一个好的设计,当我不得不这样做时我总是感到畏缩。 但现在有了 stackoverflow,所以我终于可以问了!
例如,假设我在页面中有一个“登录控件”ascx 控件。 包含页面在标题栏中显示“当前登录用户”的文本标签。
假设我单击“登录”按钮,这将触发我的身份验证并登录我的用户。问题是父页面中的文本标签已呈现为“没有用户登录”。 那就不好了!
通过将逻辑放入“PreRender”中,它将在用户登录后进行渲染。我只是不喜欢这样,因为这不是 PreRender 的用途。
我在这里缺少的预期最佳实践是什么? 我知道我可以在用户控件上放置一个事件处理程序,但这似乎也很笨拙,因为耦合太多。
附言。 我只是以此为例。 我已经多次遇到过这个问题,所以请不要回复告诉我如何实现登录!
When I have a child .ASCX control that needs to affect something in the parent page I'm not completely sure how I am supposed to handle the event flow in the case where I need to update something in the parent page.
What I've always ended up doing is putting logic into the 'Pre_Render' event handler.
Since this is processed after any child .ascx controls are processed I can be sure to render the ASPX page correctly before it displays. I just dont think this is a good design and I've always cringed when I've had to do it. But now there is stackoverflow so i can finally ask it!
For instance lets say I have a 'login control' ascx control in a page. The containing page displays a text label in the header bar for 'current logged in user'.
Lets say I click the 'login' button, which will then trigger my authentication and log my user in. The problem is that the text label in the parent page has already been rendered as 'No user logged in'. Thats no good!
By putting the logic into 'PreRender' it will be rendered after the user has logged in. I just dont like this because thats not what PreRender is for.
What is the intended best practice here that I'm missing? I know I could put an event handler on the user control, but that seems clumsy too because there'd be too much coupling.
PS. I'm just using this as an example. I'd had this problem numerous other times so please dont reply telling me how to implement login !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的 ascx.cs 中:
在您的 page.aspx.cs 中:
In your ascx.cs:
In your page.aspx.cs: