从会话返回值
只是一个小问题,因为我找不到解决我的问题的答案。
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您没有将
QuestionnaireName.Text
放入 Page1 上的会话中,而是放入了一个整数。如果您需要实际的文本属性,请将其放入会话中然后您可以检索它
这揭示了有关会话的一些信息。对象以
object
类型存储,在使用前检索它们后,您需要将它们转换为正确的类型。例如,要检索放入 Session 中的整数,您可以编写You didn't put
QuestionnaireName.Text
into Session on Page1, you put an integer. If you need the actual text property, put that into sessionAnd then you can retrieve it
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你正在做
On Page1。在第 2 页上,您可以执行以下操作:
注意
刚刚意识到您在会话中放入了一个 int ,但您似乎想在会话中存储文本并检索它。原理是一样的。
You are doing
On Page1. On Page2 you can just do:
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.
只需这样做:
问候
Just do this:
Regards
您可以使用 Session["QuestionnaireName"] = QuestionnaireName.Text;
在第二页中,您可以检索如下所示的值
,或者也可以放入 ViewState 中。 在表单之间传递数据
You can use Session["QuestionnaireName"] = QuestionnaireName.Text;
in the second page you can retrive values like below
or else you can put in ViewState also. Passing data between forms