【问题标题】:Read Integer From Sessions从会话中读取整数
【发布时间】:2013-02-25 03:01:14
【问题描述】:

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

【问题讨论】:

  • 为什么在重定向后设置会话值?您的函数中没有逻辑。页面重定向后不会设置会话值。在重定向到其他页面之前设置它。此外,您正在设置 Label1.Text = (int)Session["points"]; .你知道 Label1.Text 会期望一个字符串,那么你为什么要为它分配“int”?????

标签: c# webforms


【解决方案1】:

不,你做得不对。

Response.Redirect 出现在 Session["points"] 设置在 newWindow 函数内部之前。

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

【讨论】:

    猜你喜欢
    • 2011-06-09
    • 1970-01-01
    • 2011-08-27
    • 2020-03-25
    • 2017-04-21
    • 1970-01-01
    • 2012-08-09
    • 2013-12-09
    • 2013-07-03
    相关资源
    最近更新 更多