A Customizable Login Server Control
page 4 of 6
by Bilal Haidar
Feedback
Average Rating: 
Views (Total / Last 10 Days): 53887/ 334

Rendering the Control

[ Download Source Code ]

Building the Control

In this section, we will discuss the CreateChildControls() method, which is used to create the interface of the login control. The building block of the Login control’s interface is an HTML table, which is used to hold all the server controls that make up this composite control. If you have a look at the CreateChildControls() method, we start by adding some HTML tags, representing the table and its rows and columns.

After that, I add the first control, which is the UserName TextBox:

    // Configure & Add UserName TextBox
    _UserName = new TextBox();
    _UserName.ID = "username";
    _UserName.Attributes.Add("autocomplete","off");
    _UserName.Attributes["onclick"] = disableCLick();
    _UserName.Attributes["onload"] = "setFocus()";
    _UserName.MaxLength= 64;
    _UserName.Width= 100;
    _UserName.CssClass ="Txt";
    _UserName.EnableViewState = false;
    _UserName.TabIndex = 1;
    this.Controls.Add(_UserName);

As you can see, the UserName TextBox has been configured and then added to the Controls collection. Next, we add a RequiredFieldValidator programmatically to ensure that the user will enter a value in that text box:

    // Configure & Add RequiredFieldValidator for UserName TextBox
    _UserNameReq = new RequiredFieldValidator();
    _UserNameReq.ID = "usernameReq";
    _UserNameReq.ErrorMessage = "*";
    _UserNameReq.ControlToValidate ="username";
    _UserNameReq.ToolTip = "Username Required";
    _UserNameReq.Display = System.Web.UI.WebControls.ValidatorDisplay.Dynamic;
    this.Controls.Add(_UserNameReq);

If you continue checking the code, you will notice that the same procedure is used to add the Password TextBox and its RequiredFieldValidator, the Error Label, and the Submit Button.

Now we check the above-created properties to know which additional features are to be displayed in the control.

First, we check the RememberMe property discussed earlier. If its value is true, we add the check box, otherwise we do not.

    // Check if rememberme checkbox is on
    int flag = 0; // flag used for setting tabindex
    if ( RememberMe )
    {
        _AutoLogin = new CheckBox();
        _AutoLogin.ID = "autoLogin";
        _AutoLogin.Checked = true;
        _AutoLogin.Text = "Remember me";
        _AutoLogin.CssClass = "LoginCheck";
        _AutoLogin.TabIndex = 3;
        this.Controls.Add(new LiteralControl("<tr><td colspan=\"2\" align=\"right\">"));
        this.Controls.Add(_AutoLogin);
        this.Controls.Add(new LiteralControl("</td><td></td></tr>\r\n"));
        flag = 1;
    }

Notice the presence of the variable named flag. This flag is used to determine the tab index of the Submit button. Suppose that the RememberMe property has a value of false, which means that the check box will not be added. In this case, the tab index of the Submit button would be 3. Suppose now that the RememberMe property has a value of true, which means that we must add the check box and give it a tab index of 3. In this case, the Submit button would need a tab index of 4. The flag variable is to help calculate the right tab index.

If you continue to check the code, you will notice that we check the value of the CreateAccount property. If its value is true, we configure and add a link to the page that allows the user to create a new account. Later on, we check the value of the ForgotPassword property. If its value is true, we configure and add a link to the page that allows the user to retrieve his password. One thing to mention here is that the URL of both pages can be configured by the page developer.

If you check the configuration section for the UserName and Password Textbox controls, you would notice the use of a private method named disableClick(), which provides the client-side script needed to prevent the user from submitting the web form by pressing the Enter key when the focus is inside either of those text boxes. This way, the form will be submitted only if the user clicks the Submit button.

Another client-side function is SetFocus, which is used to set the focus to the UserName TextBox upon loading of the page. Notice that the SetFocus() function has been added in the OnPreRender() method, which is the best place to add client-side scripts, as stated by Scott Mitchell in his article, "Injecting Client-Side Script from an ASP.NET Server Control."


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 8 and 5 and type the answer here:

User Comments

Title: Help   
Name: Alex
Date: 7/10/2008 4:54:28 PM
Comment:
Ummm... is there a way to do this without downloads?
Title: Thnaks   
Name: Nitin
Date: 2/6/2008 4:10:25 AM
Comment:
I Want to use server as database not local host
Title: thanku   
Name: rajan
Date: 11/20/2007 4:57:49 AM
Comment:
very thanks...
Title: link   
Name: wajahat
Date: 9/16/2007 11:09:20 PM
Comment:
sir, at the given link dialog box is appeared
http://tortoisesvn.tigris.org/svn/tortoisesvn/

can you help me how to append some information in password after the user entered it.

thanks
Title: Hook the Apache Server Login Dialog   
Name: wajahat
Date: 9/16/2007 11:07:08 PM
Comment:
Sir,
i have read your article, it helps me a lot. I want to hook the apache login dialog and edit some information in password. can you please explain me how to do it in C#. thanks
Title: Force event implements   
Name: Ray
Date: 5/4/2007 12:48:27 PM
Comment:
Hi,

Is there a way to force users to implement events? I wrote such a login control and I want to be able to force users to change their passwords when they expire. In order for me to do that i need to not just raise PasswordExpired event but also make sure that whoever drags this server control onto the page implements that event. I havent been able to figure out how to make events "mustimplement" in server controls. Any help would be great.

Thanks
Ray
Title: Re: Ahmed   
Name: Bilal Hadiar [MVP]
Date: 1/11/2007 1:20:11 AM
Comment:
Hello Ahmed:
There is a nice starter kit that is downloadble on the ASP.NET website. This starter kit has a small forum that you can see how they did it and learn from it!

http://www.codeplex.com/Project/FileDownload.aspx?ProjectName=TheBeerHouse&DownloadId=659

Hope this helps,
Regards
Title: wanted   
Name: Ahmed
Date: 1/10/2007 11:01:12 PM
Comment:
sir i want to make a discussion board in ASP.NET. So plz give me the complete guidance of ASP.NET coding in visual studio 2003. i like ur method of teaching and giving the language skills. its the best method of giving language knowledge. my mailing adrress is ahmad86_grt@yahoo.com
Title: Asp.net control   
Name: A.Rammohan
Date: 12/25/2006 8:33:01 AM
Comment:
very very thanks ok bye
Title: hi nice code.....   
Name: Sandeepan Kundu
Date: 9/28/2006 5:09:57 AM
Comment:
Hi.. its nice code....
I am using ASP.NET-2005 code behind vb. I want to customize the provided login control to have 2 drop down list box and a password box. and authenticate based on user's selection and password....
Please suggest.... ASAP

Sandeepan
sandeepan.kundu@tv18online.com
sandeepan.kundu@gmail.com
Title: thank u   
Name: sarath Babu
Date: 9/14/2006 8:19:37 AM
Comment:
hai,its very good artricle which tells us briefly about login controls who are new to asp.net 2.0
Title: Nice job   
Name: Eugen
Date: 7/6/2006 7:42:02 AM
Comment:
It's truly good example, nice job...
but when i'm playing with defined properties it generates an error like "this._AutoCheck is missing object reference"...
do i missed something?
Title: code for linking this login/pwd to sql database   
Name: Srinivas
Date: 6/2/2006 2:02:17 AM
Comment:
i want to get this connected to sql database...
sothat i can check the pwd with the database i hav...

thank uu
Srinivas
India
Title: gbgbhbh   
Name: vffvggfv
Date: 2/24/2006 10:18:43 PM
Comment:
my sever doesmt work
Title: VWD   
Name: Rizwan Ghani
Date: 8/2/2005 5:01:45 AM
Comment:
Hi Bilal,

I am very new with VWD. I have an MS access database application for almost 40 users. This application can be accessed from outside of the office. Now i am thinking to develop a webbased interface to access this database. I don't want to use SQL database. Please could you tell me what are the good steps to make this happen.

Thanks in advance.

Rizwan Ghani
From Holland
E-Mail:rizwan@xs4all.nl
Title: Thanks for useful info   
Name: Yevgeniy
Date: 8/2/2005 1:49:07 AM
Comment:
I find it really interesting. Thanks.
Title: Please keep up this great job   
Name: Kapil Goyal
Date: 8/1/2005 4:49:05 AM
Comment:
Hello, Please keep up this great job. THIS IS WHAT IS REQUIRED TO BE DONE. THANKS
Title: Reply to AM   
Name: Bilal Haidar [MVP]
Date: 7/30/2005 5:35:11 PM
Comment:
Hi:
Well, no need to add any dll to your Bin folder. Just right click on the toolbox, add an item, browse for the dll you have, then you can click and drag the control from your toolbox to the page you are using.
Hope that helps,
Title: how is it?   
Name: am
Date: 7/30/2005 1:52:29 PM
Comment:
Dear BIlal,,
Soory i'm new on this .Net
just want to ask.
could you pls give me the steps howt o implement this
cause, after i add the both dll into my Bin folder..
but there is no login control add into my VS.Net
Title: Thank you !   
Name: Ti Shifu
Date: 7/29/2005 11:29:23 PM
Comment:
I have seen your article. I felt it is best help for me !
I am chinese , hope that I could make friends withs you!
I think you that you known the membership in .Net 2.0. And I have many questions which I don't know and you can give me answer.
My Msn:qclineceo@hotmail.com
Title: Well done   
Name: Jungalist
Date: 7/29/2005 11:15:08 PM
Comment:
Nice article. Very well put together. I am used to vague tutorials that assume the reader knows a lot more than I do. This one takes the time to explain all the details. Thank-you.
Title: Good Article   
Name: Adnan
Date: 7/17/2005 2:59:12 PM
Comment:
I have just started working in C # so i need some help in doing some project in c# .
please contact me @ madnanbaig@gmail.com
Title: Reply: code in vb   
Name: Bilal
Date: 6/23/2005 2:08:54 AM
Comment:
Ok, I got you. You can convert the code from C# to VB.NET, check this link:

http://www.developerfusion.com/utilities/convertcsharptovb.aspx

Regards
Title: code in vb   
Name: robert clancy
Date: 6/22/2005 9:49:21 PM
Comment:
Thanks, I realise that. What I would like is not to run it or use it, but to examine the code written in vb instead of C#
Title: Reply: code in vb   
Name: Bilal
Date: 6/22/2005 3:01:29 AM
Comment:
Hi Robert:
I beleive if you compile the project files accompanying the article into a dll you would be able then to add a reference to the dll you created and you can see the login control added to your VS.NET toolbox.

Hope that helps.
Title: code in vb   
Name: robert clancy
Date: 6/22/2005 2:16:50 AM
Comment:
Because I don't know C#, I would love to see the code in vb. Has anybody provided this?

Product Spotlight
Product Spotlight 
Learn More
.NET Tools
asp.net shopping cart
asp.net chart control






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 12/4/2008 3:04:50 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search