从会话中读取整数

时间:2013-02-25 03:01:15

标签: c# webforms

我正在尝试将全局变量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>

1 个答案:

答案 0 :(得分:1)

不,你做得不对。

Response.Redirect函数内设置Session["points"]之前发生

newWindow

首先,您应该重新排列这些行,但您也应该从某个地方调用该函数。