.NET 页面周期中事件触发得不够早

发布于 2024-07-16 07:25:47 字数 1291 浏览 9 评论 0原文

我有一个页面(表单),上面有许多用户控件,我试图用一个按钮来保存所有内容。 UserControls 实际上是嵌套的,所以我想以某种方式向每个 UC 发出信号,告知它应该保存自身,而不是以某种方式将所有内容包装到一个事件中或让单个事件触发一系列保存事件。

我的计划是使用静态类(支持 这个代码的答案):

public static class RequestScopedFormData
{
    private const string save_key = "request_form_is_saving";

    public static bool FormIsSaving
    {
        get
        {
            object o = HttpContext.Current.Items[save_key];
            return Convert.ToBoolean(o);
        }
        set
        {
            HttpContext.Current.Items[save_key] = value;
        }
    }
}

理想情况下,我的 MasterPage 上的按钮将设置 RequestScopedFormData.FormIsSaving = true,并且当 .NET 构建页面及其用户控件时,它们会知道对自己执行保存。

我遇到的问题是,我无法在页面生命周期中尽早触发事件来保存用户控件。 即使我将保存代码移至 PreRender 事件,并将保存 ImageButton 移至页面本身(而不是 MasterPage),我也无法在 UC 保存检查之前将 FormIsSaving 设置为 true。

发生的事情是这样的:

  • Page Page_Load
  • MasterPage Page_Load
  • UC Page_Loads 和 PreRenders(它们 是混合的,它之前预渲染了一些 它加载其他)
  • MasterPage SaveButton_Click 事件 (这是我设置班级的地方 变量)
  • MasterPage PreRender
  • Page PreRender

因此,不幸的是,SaveButton_Click 方法在加载 UC 之后发生,因此它们永远不会保存。

I have a Page (form) that has many UserControls on it, and I'm trying to have a single button to save everything. The UserControls are actually nested, so I wanted to somehow signal to each UC that it should save itself, instead of somehow wrapping everything into one event or having a single event trigger a cascade of save events.

My plan was to use a static class (props to this answer for the code):

public static class RequestScopedFormData
{
    private const string save_key = "request_form_is_saving";

    public static bool FormIsSaving
    {
        get
        {
            object o = HttpContext.Current.Items[save_key];
            return Convert.ToBoolean(o);
        }
        set
        {
            HttpContext.Current.Items[save_key] = value;
        }
    }
}

Ideally a button on my MasterPage would set RequestScopedFormData.FormIsSaving = true, and as .NET builds the Page and its UserControls, they would know to perform a save on themselves.

The problem I'm having is that I can't get an event to fire early enough in the Page lifecycle to save the UserControls. Even if I move the saving code to the PreRender event, and I move the save ImageButton to the Page itself (instead of the MasterPage), I cannot get the FormIsSaving set to true before the UC saving check.

It happens something like this:

  • Page Page_Load
  • MasterPage Page_Load
  • UC Page_Loads and PreRenders (they
    are mixed, it prerenders some before
    it loads others)
  • MasterPage SaveButton_Click event
    (this is where I set my class
    variable)
  • MasterPage PreRender
  • Page PreRender

So unfortunately the SaveButton_Click method happens AFTER the UCs are loaded, so they never save.

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

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

发布评论

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

评论(3

朱染 2024-07-23 07:25:47

更改您的 @Page 指令以包含 Trace="true" - 这将为您提供有关所调用内容的完整诊断,并允许您确定所需生命周期部分的最佳方法。

CHange your @Page directive to include Trace="true" - this will give you full diagnostics on what is being called and will allow you to identify the best method for the part of the life-cycle you need.

哀由 2024-07-23 07:25:47

您还可以检查回发是否是由保存按钮引起的:

Request.Form["__EVENTTARGET"].Contains({YourSaveButton}.UniqueID)

表单变量在页面生命周期的早期可用。

You can also check if postback was caused by the save button:

Request.Form["__EVENTTARGET"].Contains({YourSaveButton}.UniqueID)

The form variables are available earlier in the page life cycle.

2024-07-23 07:25:47

您的控件都是从同一个接口派生的吗?或者您可以为它们添加一个通用接口吗?

然后,在保存按钮方法中,您可以迭代页面上的所有控件,然后对于与您的界面匹配的项目,调用 Save 方法。

例如

foreach (Control cntrl in Page.Controls)
{
  if (cntrl is IMySavableControl)
  {
     cntrl.Save();
  }
}

Are your controls all derived from the same interface, or could you add a common interface to them?

Then in your save button method you could iterate through all controls on your page, then for the items that match your interface, call the Save method.

E.g.

foreach (Control cntrl in Page.Controls)
{
  if (cntrl is IMySavableControl)
  {
     cntrl.Save();
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文