Skip to main content
 首页 » 编程设计

c#之如何在 asp.net 页面中的页面启动时初始化对象

2024年02月23日19leader

我需要从页面启动时初始化一个对象,并在整个特定页面中使用这些对象,我该怎么做。

//待初始化的 block

            XTContext.UserContext UContext = new XTContext.UserContext(); 
            XTContext.Context ctxt = new XTContext.Context(); 
            XTErrorCollection.ErrorCollection eContext = new XTErrorCollection.ErrorCollection(); 
            ctxt = (XTContext.Context)Cache["sessionfContext"]; 
            ctxt.eContext = eContext; 
            ctxt.uContext = UContext; 

现在我想在页面中使用ctxt 和控制事件。 我试图在页面加载时初始化它,但我无法访问 ctxt。

请您参考如下方法:

试试这个 -

public partial class YourPage : System.Web.UI.Page 
{ 
    XTContext.UserContext UContext;  
    XTContext.Context ctxt;  
    XTErrorCollection.ErrorCollection eContext; 
 
 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        UContext = new XTContext.UserContext(); 
        ctxt = new XTContext.Context(); 
        eContext = new XTErrorCollection.ErrorCollection();                        
 
        ctxt = (XTContext.Context)Cache["sessionfContext"]; 
        ctxt.eContext = eContext; 
        ctxt.uContext = UContext; 
    } 
}