Reputation: 1534
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
Upvotes: 0
Views: 3229
Reputation: 63970
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.
Upvotes: 1
Reputation: 126992
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"];
Upvotes: 5
Reputation: 15881
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
Upvotes: 0
Reputation: 2194
Just do this:
string question = Session["Questionnaire"];
if(question=="")
{
//No Value
}
else
{
ReturnQnrName.Text = question;
}
Regards
Upvotes: 0