联C#code VS Page_Load方法 [英] Inline C# code vs Page_Load method
问题描述
我工作的ASP.NET Web应用程序。结果
我有两个文件 - > example.aspx和example.aspx.cs其背后code
我想知道有什么区别将内嵌C#code。在example.aspx顶部
之间 <%
如果(会话[页]!= NULL)
{
//映射会话方法瓦尔形成元素
loadSessionData();
}
%GT;
和把code文件example.aspx.cs背后的code
的的Page_Load()方法 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
如果(会话[页]!= NULL)
{
//映射会话方法瓦尔形成元素
loadSessionData();
}
}
根据在哪里我把code中的应用程序的行为在关于会话的数据加载到表单元素不同。
任何帮助将大大AP preciated。
更新:
我想通了,为什么不同的地方,我把code表现不同应用。如果我把内联C#的方法调用时在.aspx顶不上回发运行。如果我把C#方法调用中的.aspx.cs code的的Page_Load()方法隐藏文件它不回发上运行。所以,如果我想要把方法在Page_Load方法,但不能在回发运行它,我需要把它包在
如果(!的IsPostBack)
{
loadSessionData();
}
我想执行code时,最大的区别是在时刻。当(我认为)执行内联code这是Page_Render之前的Page_Load occurres。
I am working on an ASP.NET Web Application.
I have two files -> example.aspx and its code behind example.aspx.cs
I am wondering what the difference is between putting inline C# code at the top of example.aspx
<%
if (Session["Page"] != null)
{
//method that maps session vars to form elements
loadSessionData();
}
%>
and putting the code in the Page_Load() method of the code behind file example.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Page"] != null)
{
//method that maps session vars to form elements
loadSessionData();
}
}
Depending where I put the code the application behaves different in regards to loading the session data into the form elements.
Any help on this would be greatly appreciated.
Update: I figured out why depending on where I put the code the application behaved differently. If I put the inline C# method call at the top of the .aspx it does not run on postbacks. If I put the C# method call in the Page_Load() method of the .aspx.cs code behind file it does run on postbacks. So if I want to put the method in the Page_Load method but not run it on postbacks I would need to wrap it in:
if (!IsPostBack)
{
loadSessionData();
}
I would think the biggest difference would be the moment when the code is executed. Page_Load occurres before the Page_Render which is when (I think) inline code is executed.
这篇关于联C#code VS Page_Load方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!