会话返回值 [英] Return Value From Session

查看:84
本文介绍了会话返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个小问题,因为我找不到解决我问题的答案.

Just small question as I cannot find an answer that resolves my problem.

我有一个ASP页面,该页面将输入发送到数据库,然后创建一个会话.我想知道如何在另一个 aspx页面上的会话中返回某个值.

I have an ASP page which sends input to a database and then creates a session. I would like to know how to return a certain value in that session on another aspx page.

page1.aspx.cs [创建会话]

page1.aspx.cs [creates session]

public partial class new_questionnaire : System.Web.UI.Page
    {
        OscarSQL c;

            protected void Page_Load(object sender, EventArgs e)
            {  
                c = new OscarSQL();
            } // End Page_Load

            //////    Button Method    //////        
            protected void NewQnrButton_Click(object sender, EventArgs e)
            {
                // Check if the input fields are empty.
                if (QuestionnaireName.Text == "" || CustomerID.Text == "" || NumberOfQuest.Text == "")
                {
                    Error.Text = "Please enter a name for your questionnaire.";
                }
                // Parse input values to OscarSQL.
                else
                {

                    int testRet = c.InsertQuestionnaire(QuestionnaireName.Text, Int32.Parse(CustomerID.Text), Int32.Parse(NumberOfQuest.Text));
                    Session["Questionnaire"] = testRet;
                    Confirm.Text = "Your questionnaire has been named " + QuestionnaireName.Text;
                    Response.Redirect("~/add_questions.aspx");
                }

            } // End NewQNRButton_Click

        } // End new_questionnaire

Page2.aspx.cs [想在此处解析值]

Page2.aspx.cs [Would like value to be parsed here]

namespace OSQARv0._1
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //ReturnQnrName.Text here?

        }
    }
}

我希望将 Page1 中的QuestionnaireName.Text值返回给 Page2

I would like the value QuestionnaireName.Text from Page1 to be returned to ReturnQnrName.Text on Page2

推荐答案

您没有将 QuestionnaireName.Text 放到Page1的Session中,而是放了一个整数.如果您需要实际的text属性,请将其放入会话

You didn't put QuestionnaireName.Text into Session on Page1, you put an integer. If you need the actual text property, put that into session

Session["theText"] = QuestionnaireName.Text;

然后您可以检索它

string text = (string)Session["theText"]; 

这揭示了有关Session的一些信息.对象的存储类型为 object ,一旦使用它们,就需要将它们转换为正确的类型.例如,要检索放入Session中的整数,应编写

This reveals something about Session. Objects are stored of type object, you need to cast them to their correct types once you retrieve them before use. For example, to retrieve the integer you put into Session, you would write

int questionnaire = (int)Session["Questionnaire"];

这篇关于会话返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆