-2

我正在尝试将全局变量 Label1.Text 保存到会话并在 page_load 上将其读回。我在这里传递了整数:

Session["points"] = TotalPoints.Label1;

我试图在这里读回来。我究竟做错了什么?

Label1.Text = (int)Session["points"];

所有代码如下:

    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = (string)Session["points"];
    }

    public class TotalPoints
    {
        public static int Label1;
    }

    public void Validations()
    {
        if (TextBox1.Text == "20")
        {
            Image5.Visible = true;
            Image6.Visible = false;
            TotalPoints.Label1 += 1;
        }
        else
        {
            Image5.Visible = false;
            Image6.Visible = true;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Validations();
    }

    protected void newWindow(object sender, EventArgs e)
    {

        int next = new Random().Next( 3 ) + 1;
        Response.Redirect(string.Format( "Question{0}.aspx", next ));
        Session["points"] = TotalPoints.Label1;
    }

</script>

<html>
<form id="form1" runat="server">
        <asp:Image ID="Image1" runat="server" Height="60px" ImageUrl="http://www.ixl.com/qgen_res/sets_of_objects/popsicles_100.gif" Width="107px" />
        <asp:Image ID="Image2" runat="server" Height="60px" ImageUrl="http://www.ixl.com/qgen_res/sets_of_objects/popsicles_100.gif" Width="107px" />
        <asp:Image ID="Image3" runat="server" Height="60px" ImageUrl="http://www.ixl.com/qgen_res/sets_of_objects/popsicles_100.gif" Width="107px" />
        <asp:Image ID="Image4" runat="server" Height="60px" ImageUrl="http://www.ixl.com/qgen_res/sets_of_objects/popsicles_100.gif" Width="107px" />
        <br />
        How many popsicles are there?&nbsp; Count by fives. <br />
        <br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="button1_Click"/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Image ID="Image5" runat="server" Height="30px" ImageUrl="http://www.lookseeedit.com/resources/tick.jpg" Width="30px" Visible="False" />
        <asp:Image ID="Image6" runat="server" Height="30px" ImageUrl="http://star-w.kir.jp/grp/9/red-cross-wrong-i19.svg" Width="30px" Visible="False" />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Label ID="Label1" runat="server" /> </asp:Label>
        <asp:Label ID="Label2" runat="server" Text="/10"></asp:Label>
        <br />
        <br />
        <asp:Button ID="Button2" runat="server" Text="Next Question" OnClick="newWindow"/>
    </form>
</html>
4

1 回答 1

1

No, you're not doing it right.

Response.Redirect occurs before Session["points"] is set inside of the newWindow function.

To start, you should rearrange those lines, but you should also call that function from somewhere too.

于 2013-02-25T03:09:05.327 回答