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.

Recommended Answers

All 9 Replies

>But how can I do it here?

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

What language are you using for your code behind?

>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.

What language are you using for your code behind?

Visual Studio C#.

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";

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();     
    }

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!

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.