Reputation: 137
For example, I have
Session["PatientName"] = patientNameTextBox.Text;
After information is entered into the textbox
, a button will be clicked to save the session but I'm not quite sure how to do that.
Any help is appreciated. Thanks :).
Upvotes: 0
Views: 14296
Reputation: 223227
If you place the code against Button_Click event, then the above line Session["PatientName"] = patientNameTextBox.Text;
would save the Text
value in the session. To retrieve it back you can do:
string patientName = Session["PatientName"] != null ? Session["PatientName"].ToString()
: ""; //or null
Remember not to store too much information in sessions, since they are maintained on server for each user.
Upvotes: 3
Reputation: 2140
This is how you can save a session when a button is clicked:
protected void buttonSaveSession_Click(object sender, EventArgs e)
{
string patientName = textBoxPatientName.Text;
Session["PatientName"] = patientName;
}
This is how you can check or someone has a session:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["PatientName"] != null)
{
//Your Method()
}
}
Upvotes: 0
Reputation: 12944
You can check if the value is inside the Session by doing this:
if (Session["PatientName"] != null)
...
You can retrieve the value by doing this:
// Remember to cast it to the correct type, because Session only returns objects.
string patientName = (string)Session["PatientName"];
If you are not sure if there is a value inside and you want a default value, try this:
// Once again you have to cast. Use operator ?? to optionally use the default value.
string patientName = (string)Session["PatientName"] ?? "MyDefaultPatientName";
To put your answer back in a textbox or label:
patientLabel.Text = patientName;
Upvotes: 1