[ Download Source Code ]
Public Properties
For the Login control to be customizable, some of the previously listed private fields need to be made accessible. For that, we create several properties which make it possible for the page developer to get and set those private fields.
The Login control exposes ten properties, some of which will now be explained in detail.
UserName Property
The first property to explain is the UserName property:
/* Properties */
[Bindable(false),
Category("TextBoxes"),
DefaultValue(""),
Description("The username textbox value")]
public string UserName
{
get
{
this.EnsureChildControls();
return HttpContext.Current.Server.HtmlEncode(_UserName.Text);
}
set
{
this.EnsureChildControls();
_UserName.Text = value;
}
}
As you can see, in the get section of the above property, we start by calling the EnsureChildControls() method. This method is called to make sure the current Login control has child controls, which it does by first checking the value of the ChildControlsCreated property. If this property is false, meaning that the child controls are not created, then the CreateChildControls method is called so that the child controls will be created. Otherwise, if the ChildControlsCreated property returns true, meaning that the child controls have been created, we can access and work with those child controls. In the get section of this property, after ensuring that the UserName TextBox has been created, we then access its Text value and return it.
In the set section, we ensure the child control creation in a similar way, and then set the Text property of the UserName TextBox.
The Password and ErrorLabel properties can be explained the same way as the UserName property. For that reason, we will not go into the details of those properties.
IsChecked Property
The IsChecked property is defined as follows:
[Bindable(false),
Category("Appearance"),
DefaultValue(""),
Description("Returns whether the autologin checkbox is checked or not")]
public bool IsChecked
{
get
{
this.EnsureChildControls();
if ( _AutoLogin.Checked == true )
return true;
/* Not Checked */
return false;
}
set
{
this.EnsureChildControls();
_AutoLogin.Checked = value;
}
}
The above property takes care of the Remember Me CheckBox, returning a Boolean value indicating whether the check box is checked or not.
CreateAccount Property
The CreateAcount property is defined as follows:
[Bindable(false),
Category("Appearance"),
DefaultValue(""),
Description("Enable Create Account or not?")]
public bool CreateAccount
{
get
{
object enableCA = this.ViewState["EnableCreateAccount"];
if (enableCA != null)
{
return (bool) enableCA;
}
/* Default don't allow create account */
return false;
}
set
{
this.ViewState["EnableCreateAccount"] = value;
}
}
This property of the Login control enables the page developer to show or hide the link to the page where the user can create a new account.
In the get section, we get the value of the EnableCreateAccount from ViewState, as we are using ViewState to persist the value of the CreateAccount property across postbacks. If the value is different from null, we cast and return the value, otherwise we return false. This means that the CreateAccount link is hidden by default, and visible only if the developer explicitly enables it.
In the set section, we store the value of this property in ViewState.
The ForgotPassword and RememberMe properties can be explained in the same way as the above CreateAccount property. The ForgotPassword is used to configure whether we show or hide the link to the page where the user can retrieve his password. The RememberMe property is used to configure whether we show or hide the check box used to remember the login of a user.
UsernameClientID Property
The last property is usernameClientID, which returns the ClientID of the UserName TextBox. This property is used by the client-side SetFocus function, which I will discuss later.
[Bindable(false),
Category("Appearance"),
DefaultValue(""),
Description("Get username client Id")]
public string usernameClientID
{
get
{
this.EnsureChildControls();
return _UserName.ClientID;
}
}