This is a simple demonstration of managing user account information on a server using ASP.NET. I decided that this might be a good way to get my feet wet in the whole ASP.NET environment, and the trials that I went through to get this far, might be of interest (and amusement!) to everyone else.
I am very much indebted to the numerous people on CP, who have helped answer some of my very basic questions. You all know who you are, and I thank you very much for your help.
This project demonstrates the use of:
- Managing username, password, and configuration options in an MS Access database;
HttpRequest.Cookies to remember the user's login name;
- Use of
HttpSessionState to retain the user ID between web pages
The Journey into ASP.NET land
While embarking on this journey, I encountered a variety of "interesting" problems to solve. What follows are my notes regarding the things I learned.
I wanted to use the HTML password control for passwords, since it displays "dots" for the password text. I couldn't find any web form control that did this. I'm used to the MFC edit box which has a password style. It doesn't seem that the web form edit control has this capability. I could be wrong! In any case, using HTML controls is interesting because to do anything useful with the control in the C# text, you have to convert it into a "server control", which is accomplished quite easily by right clicking on the HTML control and selecting "run as server control". Of course, it took about an hour to figure this out!
After I set up my first login screen, I noticed the tab order was wrong. So, silly me, I went looking around the IDE for a "tab order" wizard or menu command like the one for MFC. I couldn't find it! So, instead, I moved the controls around directly in the HTML. That was fairly straight forward. Later on, I discovered the
TabIndex property for control. But, since I had my controls all ordered correctly in the HTML by then, I never played with the
TabIndex. Personally, I'd rather get them ordered right in the HTML.
Setting the Control ID
It's most helpful if you set the ID's of the web form control first, before creating event handlers. Otherwise, you'll end up with some non-descript name and you'll have to edit the box titled "Web Form Designer generated code" to change the names of the event handlers, etc. Not too painful, but doing it right the first time is very helpful.
I had an interesting time of getting the "Welcome to my web site" text centered to the size of the panel. I couldn't find a way to center justify the web form label control, so I created an HTML label, set it as "Run As Server Control", then changed the style, etc. Is there a better way?
An annoying thing
The thing that's annoying about these HTML controls is that the method for setting text is different. For example, label text has to be set with the
InnerText method, while edit box text is set with the
Value method. The inconsistency makes life for beginners harder.
Databases and folder permissions
I pulled my hair out for about a day trying to figure out why I could query the database, but I couldn't update it. A friendly Code Project member replied to my message and pointed out that I needed to put the database into a separate folder that had read/write permission (among other things, so that the .LDB file could be created). I had been playing with permissions in the IIS to no avail. Doing this made everything work, except that originally, my password field was called
PASSWORD, which worked fine in queries but not updates.
State, Persistence, and Server vs. Client
I must say, this whole web form stuff being stateless is quite an interesting thing to deal with. You really have to change the style in which you program, and therefore, think, about persistence, etc. Also, I'm learning that I have to segregate my thinking into server vs. client side functionality. So, for example, I was trying to figure out how to set the focus to the user name edit control using a server side method. Posing this question on CP provided me with the answer that, this is a client side thing to do. A code example was kindly provided.
As a veteran MFC/C++ programmer, even having segregated client vs. server database functionality, I find it very hard to create a mental image of what happens on the server and what happens on the client. I think this confusion is aided by the fact that ASP.NET makes this line fuzzier, with event handlers, etc.
I'm not going to do any code snippets here (unless requested). You can peruse the code yourself by downloading the project. It's quite straightforward. All of the code is more or less copied out of the MSDN help and the excellent .NET Framework Essentials from O'Reilly Press, and of course from numerous posts on CP. I've just glued it all together to make a simple demonstration and added some prototypes of classes that might be useful to me later on.
If anyone is interested in some controversial stuff, you can read my comments in the code about how I implemented the database interface. Don't look for encryption on the passwords or a lot of other bells and whistles. This is intended to be a demonstration program only, something of a test-bed as I get by ASP.NET, feet wet.