.net页面事件触发订单更改

时间:2022-05-26 15:52:41

Ok this is a really annoying bug that I have been having issues with all morning!.

好的,这是一个非常烦人的错误,我整个上午一直有问题!

I have a custom control that we have used on many project that has properties that are set and stored in Viewstate by the calling pages onload. the control sets up childcontrols with propertes on the CreateChildControls() method of the custom control.

我有一个自定义控件,我们在许多项目中使用了自定义控件,这些控件具有通过调用页面onload在Viewstate中设置和存储的属性。该控件在自定义控件的CreateChildControls()方法上设置具有属性的子控件。

Normally as usual on a postback the Page_Load event is fired then the CreateChildControls method of the control on the page is fired.

通常在回发时会触发Page_Load事件,然后触发页面上控件的CreateChildControls方法。

The strange thin though is we have a login system (custom membership provider) on the site and when a user is logged in the opposite happens first the CreateChildControls() method fires then the Page_Load so the control properties are wrong (set from the previous postback)

奇怪的是,我们在网站上有一个登录系统(自定义成员资格提供程序),当用户登录时,相反的情况发生CreateChildControls()方法然后触发Page_Load,因此控件属性是错误的(从之前的回发中设置) )

How could the events be firing in a different order? I thought all page events happened in the same order no matter what and I don't see how being logged in would change that order.

怎么可能以不同的顺序发射事件?我认为无论如何,所有页面事件都以相同的顺序发生,我不知道如何登录会改变该顺序。

UPDATE: It seems the issue is I'm not calling EnsureChildControls() but I'm not sure where it should be called? If several properies are set on the control which are used in setting up the child controls when should I call EnsureChildControls(), I guess I don't fully understand what EnsureChildControls() does?

更新:似乎问题是我没有调用EnsureChildControls(),但我不确定应该在哪里调用它?如果在控件上设置了几个用于设置子控件的属性我何时应该调用EnsureChildControls(),我想我还不完全理解EnsureChildControls()的作用?

2 个解决方案

#1


CreateChildControls is called whenever the ASP.NET page needs them. There is no specific point in the page cycle for that. It can happen in the Init event, it can happen in the Load event. If you want to make sure your child controls are available, then call EnsureChildControls() method of your control. You can do that in the control's Init event to make sure you have child controls through the whole lifecycle or you can do it whenever you need a reference to one of the child controls - e.g. in the getter/setter of a property of your control.

只要ASP.NET页面需要它们,就会调用CreateChildControls。页面循环中没有特定点。它可能发生在Init事件中,它可能发生在Load事件中。如果要确保子控件可用,请调用控件的EnsureChildControls()方法。您可以在控件的Init事件中执行此操作,以确保在整个生命周期中都有子控件,或者您可以在需要引用其中一个子控件时执行此操作 - 例如在您的控件的属性的getter / setter中。

#2


When creating properties of a server/user control that need access to contained child controls I use the following:

在创建需要访问包含的子控件的服务器/用户控件的属性时,我使用以下命令:

public Whatever SomeProperty
{

    get
    {
        EnsureChildControls();
        <more code here>
    }
    set
    {
        EnsureChildControls();
        <more code here>
    }
}

This ensures your control consumers are free to work with your control at various stages of the page lifecycle.

这可确保您的控件使用者可以在页面生命周期的各个阶段*地使用您的控件。

#1


CreateChildControls is called whenever the ASP.NET page needs them. There is no specific point in the page cycle for that. It can happen in the Init event, it can happen in the Load event. If you want to make sure your child controls are available, then call EnsureChildControls() method of your control. You can do that in the control's Init event to make sure you have child controls through the whole lifecycle or you can do it whenever you need a reference to one of the child controls - e.g. in the getter/setter of a property of your control.

只要ASP.NET页面需要它们,就会调用CreateChildControls。页面循环中没有特定点。它可能发生在Init事件中,它可能发生在Load事件中。如果要确保子控件可用,请调用控件的EnsureChildControls()方法。您可以在控件的Init事件中执行此操作,以确保在整个生命周期中都有子控件,或者您可以在需要引用其中一个子控件时执行此操作 - 例如在您的控件的属性的getter / setter中。

#2


When creating properties of a server/user control that need access to contained child controls I use the following:

在创建需要访问包含的子控件的服务器/用户控件的属性时,我使用以下命令:

public Whatever SomeProperty
{

    get
    {
        EnsureChildControls();
        <more code here>
    }
    set
    {
        EnsureChildControls();
        <more code here>
    }
}

This ensures your control consumers are free to work with your control at various stages of the page lifecycle.

这可确保您的控件使用者可以在页面生命周期的各个阶段*地使用您的控件。