会话内容不会出现在第二页的内容中

时间:2010-12-20 07:52:59

标签: asp.net vb.net visual-studio-2008 session

我把这个会话放在我希望它们显示的页面的页面加载中,但会话内容出现在我的网站顶部而不是内容中有什么问题????

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Response.Write("<h1 align=center>Radiation Calculator</h1>")
    Response.Write("<br>Hi Mr/Mrs " & Session("Username"))
    Response.Write("<br>The following results have been sent to your Email " & Session("Email"))
    Response.Write("<br>Air Traveling Distance " & Session("Air Traveling Disance") & " Km")
    Response.Write("<br>You have selected the following Factors: " & Session("Factor0") & " . " & Session("Factor1") & " . " & Session("Factor2") & " . " & Session("Factor3") & " . " & Session("Factor4") & " . " & Session("Factor5") & " . " & Session("Factor6") & " . " & Session("Factor7"))
    Response.Write("<br>Total Radiation " & Session("Total Radiation") & " mrem")
    Response.Write("<br>Your Life Reduction would be: " & Session("User Life Expectancy In Minutes") & "Mintues " & "  " & Session("User Life Expectancy In Hours"))



End Sub

1 个答案:

答案 0 :(得分:3)

这是因为您直接在客户端的响应流中编写。因此,您在Page Load中编写的所有内容都先写入。有关调用特定方法的信息,请参阅ASP.NET Page Lifecycle

但在大多数情况下,您不必(也不应该)直接在Response中写入。修改您的aspx-File,因此您可以使用服务器端控件,例如:

<h1 align="center">Radiation Calculator</h1>
<br>Hi Mr/Mrs <asp:Label id="lblUsername" runat="server" />
...

而不是在Page_Load方法中使用:

lblUsername.Text = Session("Username")

所以忘掉Response.Write。修改您的ASPX页面,以便在其中包含常规HTML。对内容使用服务器端控件,然后在代码后面的Page_Load方法中为控件分配正确的值。