Using C# and ASP .Net
-
Hi, Using the code-behind, I have two web pages, page 1 and page 2. The user will enter their name into TextBox1.Text on page 1, then press the submit button. Question is how do you get the data (user name) from page 1 to page 2. Please show using C# code:((
-
Hi, Using the code-behind, I have two web pages, page 1 and page 2. The user will enter their name into TextBox1.Text on page 1, then press the submit button. Question is how do you get the data (user name) from page 1 to page 2. Please show using C# code:((
Hi there, You can use the Session object, e.g. Page #1
protected System.Web.UI.WebControls.TextBox TextBox1;
private void Page_Load(object sender, System.EventArgs e)
{
if (IsPostBack())
{
Session["username"] = TextBox1.Text;
}// ... // ... // ...
}
And then in Page #2
private void Page_Load(object sender, System.EventArgs e)
{
// has the username been set?
string userName = Session["username"];// ... // ... // ...
}
Hope this helps, Andy
-
Hi there, You can use the Session object, e.g. Page #1
protected System.Web.UI.WebControls.TextBox TextBox1;
private void Page_Load(object sender, System.EventArgs e)
{
if (IsPostBack())
{
Session["username"] = TextBox1.Text;
}// ... // ... // ...
}
And then in Page #2
private void Page_Load(object sender, System.EventArgs e)
{
// has the username been set?
string userName = Session["username"];// ... // ... // ...
}
Hope this helps, Andy
-
Hi Andy, Thank for your help! I'm getting an error: can't implicitly convert type 'object' to 'string'. Page 2 string username = Session["username"];
string username = Session["username"] as string;
Have a look at my latest article about Object Prevalence with Bamboo Prevalence.
-
string username = Session["username"] as string;
Have a look at my latest article about Object Prevalence with Bamboo Prevalence.
-
Hi there, You can use the Session object, e.g. Page #1
protected System.Web.UI.WebControls.TextBox TextBox1;
private void Page_Load(object sender, System.EventArgs e)
{
if (IsPostBack())
{
Session["username"] = TextBox1.Text;
}// ... // ... // ...
}
And then in Page #2
private void Page_Load(object sender, System.EventArgs e)
{
// has the username been set?
string userName = Session["username"];// ... // ... // ...
}
Hope this helps, Andy
Why not use a form and on page 2 do the following:
string userName = Request.Params["TextBox1"]
Storing information on the session is not wise as it consumes more server memory. theJazzyBrain
Excellence is not an act, but a habit!
Aristotle
-
Why not use a form and on page 2 do the following:
string userName = Request.Params["TextBox1"]
Storing information on the session is not wise as it consumes more server memory. theJazzyBrain
Excellence is not an act, but a habit!
Aristotle
That would depend on what you were storing. Not wise if it was something like connection string information (although yes you could encrypt it) and then want about an object, you would have to implement serialize functionality to be able to restore the state of the object.