Creating a Login Custom Web Control
ASP.NET provides several web controls that provide rich functionality to the users. In addition to the built-in web controls ASP.NET also allows for the creation of custom web controls. This is something similar to building ActiveX controls. There are two approaches to create a custom web control. Either you build every thing from scratch or you assemble various web controls together to create a new control (sometimes called as
page lets). The first approach gives complete control of the control rendering process. The second approach is however much easy to implement and less time consuming. In this code sample we will see how to create a reusable login web control using the second approach.
Login web control
Providing a login screen is very common requirement in many web applications. Typically we use web controls like label, textbox, password and button to built such a login screen. You can encapsulate all this coding in a user control and use across multiple web applications.
The login web control exists as a .ascx file some times referred to as page let. You can add any HTML element or web control to this file. Note however that since this file is being hosted in some other form it should not contain elements like HTML,BODY and FORM. Once you create this file you have to 'register' it in the web form in which you want to use it. Then you can create as many instances of the control as you want just like any other built-in web control.
Here, we will develop such a reusable login control. We want following functionality for the control :
The developer should be able to change captions of labels e.g. User ID, Password
The developer should be able to change caption of the submit button e.g. Login
There are two ways to validate the user id and password entered by the user. One way is - you raise a event that allows the developer to write custom logic for validating the user. Other way is - you supply the database table details to the control and the control should tell you whether user is valid or not
For the sake of simplicity we will not consider any UI related requirements like changing font or color.
List of properties of Login control
- UserIDLabelText() As String : Indicates text to be displayed against user id text box
- PasswordLabelText() As String : Indicates text to be displayed against password text box
- LoginButtonText() As String : Indicates caption of login button
- ConnectionString() As String : Indicates connection string in case you are using built-in validation mechanism
- DataSource() As String : Indicates table name that contains list of user ids and password
- UserIDDataMember() As String : Indicates field from above table that contain user id
- PasswordDataMember() As String : Indicates field from above table that contain password
- Mode() As ValidationMode : Custom or In-Built validation mode
- ReadOnly Property IsValid() As Boolean : True or false in case you use built-in validation
List of Methods of the Login web control
- Private Sub Validate(ByVal uid As String, ByVal pwd As String) : Method that validates the user based on values entered in controls and sets IsValid property to True/False
List of Events raised by the Login web control
- Public Event ValidateUser (ByVal uid As String, ByVal pwd As String) : This event is raised in case mode is set to custom (default). The event receives user id and password entered in controls as event arguments that can be validated against database table or some custom mechanism can be used.
is a software consultant, trainer, author, yoga mentor, and spiritual guide having 24+ years of experience in software development, consulting, and training. He conducts instructor-led online training courses
in ASP.NET Core, ASP.NET MVC, and Design Patterns for individuals and small groups. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Ajapa Yoga
to interested individuals. To know more about him click here
Get article updates : Facebook Twitter LinkedIn