用户控件Page_Load运行两次

时间:2019-06-04 13:42:50

标签: c# load cycle

Page_Load方法在嵌入了UserControls(UC)的WebForms中运行两次。

我有一些带有用户控件的Web窗体,而另一些则没有。 在那些没有UC的页面中,Page_Load运行一次(这是预期的)。然后,我将任何UC放在其中一个WebForm中,并且Page_Load被执行了两次。

protected void Page_Load(object sender, EventArgs e)
        {
            //code when it's postback

            if (!IsPostBack)
            {
               //code when it's not postback
            }
        }

我确实希望只输入一次Page_Load。 这种现象随处可见(MasterPage,WebForms,子UC)。

我还尝试设置AutoEventWireup =“ false”并将方法附加到生命周期事件上,但是它是相同的(我认为它与它无关,但值得尝试)。

0 个答案:

没有答案