Tutorials » AspNet tutorials »

Add life to Login page


In this chapter, you will add code to the event handlers in the Login Page and add the real functionality to it.


Let us add some code in the Login page and give it some real action.

What is this page supposed to do ?



User can enter their name into the textbox and submit. Whatever name user enters, it will be saved to session variable and application variable. Once the name is added to the session variable, the textbox and button will be hidden and a welcome message will be displayed.

Add code to event handlers



Switch to design mode and double click on the button. This will add a button_click event handler in the code behind file, as shown below:

Private Sub btnProceed_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnProceed.Click

End Sub


Let us add the following code to the above event handler.


Dim name As String = txtName.Text

' store the name into the session variable.
Session("Name") = name

AddNameToApplicationVariable(name)

' We have selected a name. Display the name and disable the textboxes.
DisplayCurrentUser()


The first statement gets the name from text box and assigns to a variable. The second statement adds it into the session variable.

The statement AddNameToApplicationVariable(name) is calling a function called AddNameToApplicationVariable. This function will add the name into an ArrayList object and add the ArrayList object to an application variable so that this can be accessed by other users also.

The last statement DisplayCurrentUser() displays a welcome message and hides the panel.

Here is the code for the function AddNameToApplicationVariable()


Private Sub AddNameToApplicationVariable(ByVal name As String)
Dim nameList As ArrayList

nameList = Application("Names")

If nameList Is Nothing Then
' There is no names added to application variable yet.
' Let us create a new ArrayList

nameList = New ArrayList()
End If

' Add the name to the arry list
nameList.Add(name)

' Add ArrayList object itself to the application variable.
Application("Names") = nameList
End Sub


The first statement declares an object called "nameList" as the type "ArrayList". ArrayList is a class provided by .NET Framework. This class is similar to "Array" and provides several methods to add/remove items to the list. You can add/remove any type of objects to an ArrayList.

The second statement is retrieving the value of application variable "Names". This may be null initially. If this is null, we will create a new ArrayList object and assign to it to the variable "nameList". This is done in the statement nameList = New ArrayList()

So, now we have a valid ArrayList (either retrieved from Application variable or created new). Now we are going to add our user name into this array list. This is done in the statement nameList.Add(name). This statement will add the current name to the array list. This arraylist may already have other user names, if any other has already logged in.

The last statement Application("Names") = nameList will save this array list to the Application variable.

Note an important point here - what we save into the application variable is an ArrayList, not just a string. This ArrayList will have the names of all users currently logged in to the application.

Whenever a user log in to the application, this array list will be retrieved from the application variable, add the new user's name into the array list and save it back to the application variable.

See the code for the method DisplayCurrentUser():


Private Sub DisplayCurrentUser()
Dim name As String = Session("Name")
If name = String.Empty Then
' There is no name in the session variable. This means
' there is no name selected yet.

Return
End If

' The name is already in the session.
' This user has already entered the name.
' So, let us disable to the panel so that all controls within
' the panel will be invisible.

pnlName.Visible = False

' Display the name in the message label.
lblMessage.Text = "Welcome " & name
End Sub


The above method will check if the name is already in the session variable. If it is there, it will understand that the user has already logged in. Then it will display a welcome message and hide the panel "pnlName". When a panel is hidden, all controls inside the panel also will be hidden. So, after a user is logged in, the textbox and button will not be visible any more.

Let us do a last step. Switch to design mode and double click on the form. This will create a Form_Load event handler. Call the method DisplayCurrentUser() from the Form_Load event, to make sure whenever we load this page, it will check if the user is already logged in or not and display controls accordingly.


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
MyBase.Load
' When we load the page, first check if the user name is already in session.
DisplayCurrentUser()
End Sub


Launch the page



After you finish the above code, compile and launch the page. You will see the page in action. After you enter a name and submit, you will see the welcome message.

Read the code several times and clearly understand how it works.



Next Chapter: Display currently online users
Previous Chapter: User Tracker: Login Page
More Chapters: ASP.NET Tutorials
More Tutorials: Tutorial Index



Top Contributors
Today
    Last 7 Daysmore...

    Awards & Gifts

    Online Members

    More...
     
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India