当 ASCX 子控件需要触发更改时,更新 ASPX 页面的最佳方法是什么?

发布于 2024-07-07 22:43:26 字数 588 浏览 7 评论 0原文

当我有一个需要影响父页面中某些内容的子 .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 技术交流群。

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

发布评论

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

评论(1

苦行僧 2024-07-14 22:43:26

在您的 ascx.cs 中:

public delegate void NavigateEventHandler(int PID); // if you want a custom handler
public event NavigateEventHandler onNavigate;

在您的 page.aspx.cs 中:

protected void Page_Init(object sender, EventArgs e) {
    eSelector1.onNavigate += new PostSelector.NavigateEventHandler(eSelector1_Navigate); }
public void eSelector1_Navigate(int PID) {
    eSelector1.PopulateComments(eSelector1.m_PID); }

In your ascx.cs:

public delegate void NavigateEventHandler(int PID); // if you want a custom handler
public event NavigateEventHandler onNavigate;

In your page.aspx.cs:

protected void Page_Init(object sender, EventArgs e) {
    eSelector1.onNavigate += new PostSelector.NavigateEventHandler(eSelector1_Navigate); }
public void eSelector1_Navigate(int PID) {
    eSelector1.PopulateComments(eSelector1.m_PID); }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文