[ Download Source Code ]
Control Skeleton
Since we are developing a custom web server control, the basic step is to create a class that inherits from WebControl and implements INamingContainer, as follows:
[DefaultProperty("Text"),ToolboxData("<{0}:Login runat=server></{0}:Login>")]
public class Login : WebControl, INamingContainer
{
// Login control code
}
Why Inherit from WebControl?
WebControl is the building block for all web server controls. By inheriting from this class, we will be able to access and override all the methods in that class, instead of having to rewrite them from scratch.
Why Implement the INamingContainer Interface?
When a control implements this interface, its behavior is to ensure that the ClientID property of each child control is unique within the Web Form. (For more information about this interface, view the MSDN documentation.)
Private Fields
The following private fields are added to the control skeleton.
/* Private Fields */
private Button _SubmitButton;
private TextBox _UserName;
private TextBox _Password;
private Label _ErrorLabel;
private CheckBox _AutoLogin;
private HyperLink _CreateAccount;
private HyperLink _ForgotPassword;
private RequiredFieldValidator _UserNameReq;
private RequiredFieldValidator _PasswordReq;
In the code above, we can see the different server controls that are used to build our custom login control:
- A Button, used to submit the form.
- Two TextBoxes, used for obtaining the username and password.
- An Error Label, used to display any error messages based on the input of the user.
- A CheckBox, used to determine if the user wants a persistent cookie.
- Two Hyperlinks, used to provide links to pages where the user can create a new account or retrieve a forgotten password.
- Two RequiredFieldValidators, used to validate the user input.