从会话返回值

发布于 2024-12-11 02:26:07 字数 1763 浏览 0 评论 0原文

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

我有一个 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

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?

        }
    }
}

我希望将 Page1 中的 QuestionnaireName.Text 值返回到 上的 ReturnQnrName.Text >第2页

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

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 [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 [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?

        }
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

咽泪装欢 2024-12-18 02:26:07

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

Session["theText"] = QuestionnaireName.Text;

然后您可以检索它

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

这揭示了有关会话的一些信息。对象以 object 类型存储,在使用前检索它们后,您需要将它们转换为正确的类型。例如,要检索放入 Session 中的整数,您可以编写

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

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;

And then you can retrieve it

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

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"];
谈下烟灰 2024-12-18 02:26:07

你正在做

Session["Questionnaire"] = testRet;

On Page1。在第 2 页上,您可以执行以下操作:

protected void Page_Load(object sender, EventArgs e)
{
     string questionnaire = Session["Questionnaire"] as  string;
     if(!string.IsNullOrEmpty(questionnaire ))
     ReturnQnrName.Text =questionnaire ;

}

注意
刚刚意识到您在会话中放入了一个 int ,但您似乎想在会话中存储文本并检索它。原理是一样的。

You are doing

Session["Questionnaire"] = testRet;

On Page1. On Page2 you can just do:

protected void Page_Load(object sender, EventArgs e)
{
     string questionnaire = Session["Questionnaire"] as  string;
     if(!string.IsNullOrEmpty(questionnaire ))
     ReturnQnrName.Text =questionnaire ;

}

Note
Just realized that you put an int in your session but you seemed to want to store a text in Session and retrieve it. The principle is the same.

生来就爱笑 2024-12-18 02:26:07

只需这样做:

 string question = Session["Questionnaire"];

    if(question=="")
    {
        //No Value
    }
else
{
    ReturnQnrName.Text = question;
}

问候

Just do this:

 string question = Session["Questionnaire"];

    if(question=="")
    {
        //No Value
    }
else
{
    ReturnQnrName.Text = question;
}

Regards

临走之时 2024-12-18 02:26:07

您可以使用 Session["QuestionnaireName"] = QuestionnaireName.Text;

在第二页中,您可以检索如下所示的值

                string QuestionnarireName;
                if (Session["QuestionnaireName"] != null)
                {
                    QuestionnarireName = Session["QuestionnaireName"].ToString();

                }

,或者也可以放入 ViewState 中。 在表单之间传递数据

You can use Session["QuestionnaireName"] = QuestionnaireName.Text;

in the second page you can retrive values like below

                string QuestionnarireName;
                if (Session["QuestionnaireName"] != null)
                {
                    QuestionnarireName = Session["QuestionnaireName"].ToString();

                }

or else you can put in ViewState also. Passing data between forms

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文