0

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.

5
Contributors
9
Replies
11
Views
6 Years
Discussion Span
Last Post by vuyiswamb
0

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

0

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

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

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!

0

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.

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.