954,595 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Retaining Data in ASP.NET Page

I have a simple ASP.NET page where after the initial page load the user enters a username and a password to two text boxes. Note that I can not use the built-in login control in ASP.NET, as my purpose of using this username and password is slightly different.

Now, I want the user to enter the said two values only once. Afterwards, using those two values I perform several tasks. Now here's the tricky part; since I set the TextMode property of the password text box to 'password', every time a button click occurs it clears the password field, and since every button click event triggers a page load as well, I lose the password data. But I do not want the user to keep entering the password every time he has to click a button.

How can I retain the username and password in my code? If this were a normal .NET Windows Application, I would use a global variable and assign the value the first time and I would have it available to me throughout the life cycle of my program. But how can I do it here?

Please note that this is my very first ASP.NET program, so some code sample would be greatly appreciated.

sachintha81
Junior Poster in Training
96 posts since Apr 2008
Reputation Points: 35
Solved Threads: 1
 

>But how can I do it here?

You can use Session object in asp.net to save the state/data.

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

What language are you using for your code behind?

CrappyCoder
Light Poster
37 posts since Dec 2010
Reputation Points: 36
Solved Threads: 6
 

>But how can I do it here?

You can use Session object in asp.net to save the state/data.

Can you please show me how? Some code is greatly appreciated.

sachintha81
Junior Poster in Training
96 posts since Apr 2008
Reputation Points: 35
Solved Threads: 1
 
What language are you using for your code behind?

Visual Studio C#.

sachintha81
Junior Poster in Training
96 posts since Apr 2008
Reputation Points: 35
Solved Threads: 1
 

Visual Studio is the Development Envrionment and C# is the programming language.

Normally you would use the ViewState object to save page-specific information and the Session object to save Site-specific information.

//Page Specific 
ViewState["UrlReferrer"] = Request.UrlReferrer.ToString();   
ViewState["ValueEntered"] = TextBox1.Text;  


//Website specific
Session["UserName"] = "JohnThomas";
CrappyCoder
Light Poster
37 posts since Dec 2010
Reputation Points: 36
Solved Threads: 6
 

Yelp CrappyCoder is right, the viewstate is going to load anyway. But if you are going to move to another page or just want to use sessions anyway. I put it in a button click event.

protected void Button1_Click(object sender, EventArgs e)
    {
        if (Session["userPassWord"] == null)
        Session["userPassWord"] = TextBox1.Text;

        if (Session["userPassWord"] != null)
            Label1.Text = Session["userPassWord"].ToString();     
    }
Freon22
Light Poster
42 posts since Feb 2010
Reputation Points: 12
Solved Threads: 6
 

Visual Studio is the Development Envrionment and C# is the programming language.

Normally you would use the ViewState object to save page-specific information and the Session object to save Site-specific information.

//Page Specific 
ViewState["UrlReferrer"] = Request.UrlReferrer.ToString();   
ViewState["ValueEntered"] = TextBox1.Text;  


//Website specific
Session["UserName"] = "JohnThomas";

Thanks CrappyCoder, it worked fine!

sachintha81
Junior Poster in Training
96 posts since Apr 2008
Reputation Points: 35
Solved Threads: 1
 

Yelp CrappyCoder is right, the viewstate is going to load anyway. But if you are going to move to another page or just want to use sessions anyway. I put it in a button click event.

protected void Button1_Click(object sender, EventArgs e)
    {
        if (Session["userPassWord"] == null)
        Session["userPassWord"] = TextBox1.Text;

        if (Session["userPassWord"] != null)
            Label1.Text = Session["userPassWord"].ToString();     
    }

Thanks Freon it works fine.

sachintha81
Junior Poster in Training
96 posts since Apr 2008
Reputation Points: 35
Solved Threads: 1
 

You do not need to store values, What you need to do is to wrap your controls inside an Update Panel and you will get a partial update. and your values will be retained.

vuyiswamb
Posting Whiz
312 posts since Mar 2007
Reputation Points: 31
Solved Threads: 14
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: