| home / programming / asp / quasi / 1 | [previous][next] |
|
|
Now, we close our all of our if statements, then our sub, finally the script. The following is the HTML portion of this step:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Sample Login Form</title>
</head>
<body style="font:11px arial;">
<form runat="server">
<asp:Label
| id="lblmessage" |
| id="txtMemberName" |
| id="lblname" |
| id="txtPassword" |
| id="lblpass" |
| id="butOK" |
Here, we use the asp.net elements for the text boxes and the command button, which use runat=”server”. This is also present in a form tag. This tells the server which elements are to be used when it executes the script. I added a touch of style to this basic page using the style attribute in CSS.
This completes the login page, but now we need a page to see if the user is logged in and to get some information about that person:
Success.aspx
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OLEDB" %>
<script runat="server">
Again, we set this page to debug and to set the script to run at the server. But we also requested the system.data namespace and the system.data.oledb namespace. We’re not actually going to connect to a database on this page, but I just left it there as an option.
Sub Page_Load(Src As Object, E As EventArgs)
We have a sub to put our code in, and it is set to fire when the page loads.
| if len(session("memberid")) = 0 then | ||||
| response.Redirect("/login.aspx") | ||||
| end if | ||||
Here, we check the length of the session variable “memberid” that we set on the other page. If the length is 0, meaning there is nothing there; then we know the user did not log in, so we just send them over to our login form and make sure they do so. This prevents the user from simply typeing in the address bar success.aspx without first logging in.
| lblmessage.text = "Welcome to the site " & session("memname")
& " _ & “. You have just logged in, and your member id is " _ |
||
| & session("memberid") | ||
Now, we populate a label (which outputs a <span>) with a welcome message and place the “memname” variable that we set up on the other page into our message, and grab that “memberid” and place it in there too.
End Sub
Now, we break out of our sub. The next sub will be fired when the user clicks the logout button.
| Sub logoutBtn_Click(Sender As Object, E As EventArgs) | ||
session("memname")="" |
||
| session("memberid")="" |
||
This takes the above session variables and gives them a null value of “”. Our id just so happens to have a length of 0 when we do this, so the user is now logged out of our system. As a demonstration, the user is redirected to success.aspx, the page they are currently on. Since the user is no longer logged on, they are taken back over to the login form. This happens because of the code we used earlier in this article to make sure the user had logged in before visiting our restricted page.
end sub
</script>
| home / programming / asp / quasi / 1 | [previous][next] |
Created: March 27, 2003
Revised: February 29, 2004
URL: URL: http://webreference.com/programming/asp/quasi/1