グローバル変数 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? Count by fives. <br />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="button1_Click"/>
<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" />
<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>