从会话返回值

HGo*_*mez 0 c# asp.net session return

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

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

page1.aspx.cs [创建会话]

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
Run Code Online (Sandbox Code Playgroud)

Page2.aspx.cs [希望在这里解析值]

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

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我想价值QuestionnaireName.Text从第1页返回到ReturnQnrName.Text在第2页

Ant*_*ram 5

你没有QuestionnaireName.Text在Page1上放入Session,你输入一个整数.如果您需要实际的文本属性,请将其放入会话中

Session["theText"] = QuestionnaireName.Text;
Run Code Online (Sandbox Code Playgroud)

然后你可以检索它

string text = (string)Session["theText"]; 
Run Code Online (Sandbox Code Playgroud)

这揭示了Session的一些内容.对象存储的类型object,您需要在使用之前将它们转换为正确的类型.例如,要检索放入Session的整数,您可以编写

int questionnaire = (int)Session["Questionnaire"];
Run Code Online (Sandbox Code Playgroud)