Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 扩展a.cs的aspx.cs类:为什么我可以';构造函数无法获取此值?_C#_Asp.net_.net - Fatal编程技术网

C# 扩展a.cs的aspx.cs类:为什么我可以';构造函数无法获取此值?

C# 扩展a.cs的aspx.cs类:为什么我可以';构造函数无法获取此值?,c#,asp.net,.net,C#,Asp.net,.net,我有以下代码: MainPage.cs public class MainPage : System.Web.UI.Page { public string myVar = ""; public MainPage() { } protected override void OnPreInit(EventArgs e) { myVar = "Hello"; HttpContext.Current.Items["my

我有以下代码:

MainPage.cs

public class MainPage : System.Web.UI.Page
{
    public string myVar = "";

    public MainPage()
    {
    }

    protected override void OnPreInit(EventArgs e)
    {
        myVar = "Hello";
        HttpContext.Current.Items["myVar"] = myVar;
        base.OnPreInit(e);
    }
}

public class iUserControl : System.Web.UI.UserControl
{
    protected string myVar;

    public iUserControl()
    {
        myVar = (string)HttpContext.Current.Items["myVar"];
    }
}
Example.aspx

<%@ Register src="../context/master/Exz.ascx" tagname="Exz" tagprefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <uc1:Exz ID="Exz1" runat="server" />
    </div>
    </form>
</body>
</html>

但我真的不知道为什么,有了OnLoad,它就可以工作了。你能帮我解决这个问题吗?

因为iUserControl的构造函数将在主页的OnPreInit之前执行,而OnLoad将在之后执行,因为iUserControl的构造函数将在主页的OnPreInit之前执行,而OnLoad将在之后执行

这是一个页面/控件生命周期问题。按他们开火的时间顺序:

MainPage.MainPage()

iUserControl.iUserControl()

MainPage.OnPreInit()

MainPage.OnLoad()

iUserControl.OnLoad()

这是一个页面/控件生命周期问题。按他们开火的时间顺序:

MainPage.MainPage()

iUserControl.iUserControl()

MainPage.OnPreInit()

MainPage.OnLoad()


iUserControl.OnLoad()

哦……这是一个生命周期问题。我想我会了解更多的。我还认为MainPage.MainPage()是最后一个被调用的方法(构造函数)。。。该死:)哦……这是一个生命周期问题。我想我会了解更多的。我还认为MainPage.MainPage()是最后一个被调用的方法(构造函数)。。。该死:)
public partial class interne_Example : MainPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("First : " + myVar + "<br />");
    }
}
public partial class context_master_Exz : iUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("Second : " + myVar + "<br />");
    }
}   
public class iUserControl : System.Web.UI.UserControl
{
    protected string myVar;

    public iUserControl()
    {           
    }

    protected override void OnLoad(EventArgs e)
    {
        myVar = (string)HttpContext.Current.Items["myVar"];
        base.OnLoad(e);
    }       
}