Click here to Skip to main content
15,887,350 members
Articles / Web Development / ASP.NET
Article

Composite Login Control

Rate me:
Please Sign up or sign in to vote.
1.12/5 (12 votes)
22 May 2006 29.3K   624   20   2
This article shows the implementation of Composite Server Control to provide Login features

Introduction

This  article shows the implementation of Composite Server Control. The purpose of creating this control is create a Login Control which is most commonly used in web sites. Composite control uses child controls for its functionality. It consists of Label Control to display text and TextBox Control for getting the values from user,  along with RequiredFieldValidator control to validate the data enter by user. It also contain a Button control to submit the data to server.

"userTextBox" is used to get the value for “user name” and "passTextBox" to get the “password” whose TextMode is set to “password”. Finally a button control to submit the data.

Sample image

Technical Implementation

Comp.cs (Class Library)

using System;
using System.ComponentModel;
using System.Drawing;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Controls
{
public class comp : CompositeControl
{
#region private control variables
private Label userLabel;
private Label passLabel;
private Button submitButton;
private TextBox userTextBox;
private TextBox passTextBox;
private RequiredFieldValidator userValidator;
private static readonly object EventSubmitKey = new object();
#endregion

#region Public Attributes
<P>public string userName</P><P>{</P><P>get</P><P>{</P><P>EnsureChildControls();</P><P>return userTextBox.Text;</P><P>}</P><P>set</P><P>{</P><P>EnsureChildControls();</P><P>userTextBox.Text = value;</P><P>}</P><P>}</P><P>public string Password</P><P>{</P><P>get</P><P>{</P><P>EnsureChildControls();</P><P>return passTextBox.Text;</P><P>}</P><P>set</P><P>{</P><P>EnsureChildControls();</P><P>passTextBox.Text = value;</P><P>}</P><P>}</P>[Category("Action"),Description("Fires when user click on the Submit Button")]
public event EventHandler Submit
{
add
{
Events.AddHandler(EventSubmitKey, value);
}
remove
{
Events.RemoveHandler(EventSubmitKey, value);
}
}
#endregion

#region Button Events
protected virtual void OnSubmit(EventArgs e)
{
EventHandler SubmitHandler = (EventHandler)Events[EventSubmitKey];
if (SubmitHandler != null)
{
SubmitHandler(this, e);
}
}

private void _button_Click(object source, EventArgs e)
{
OnSubmit(EventArgs.Empty);
}
#endregion

#region override CreateChildControls/Render Methods
protected override void CreateChildControls()
{
Controls.Clear();
userLabel = new Label();
userLabel.Text = "User Name";
userTextBox = new TextBox();
userTextBox.ID = "nameTextBox";
userValidator = new RequiredFieldValidator();
userValidator.ID = "validator1";
userValidator.ControlToValidate = userTextBox.ID;
userValidator.ErrorMessage = "*";
userValidator.Display = ValidatorDisplay.Static;
passLabel = new Label();
passLabel.Text = "Password";
passTextBox = new TextBox();
passTextBox.ID = "passTextBox";
passTextBox.TextMode = TextBoxMode.Password;
submitButton = new Button();
submitButton.ID = "button1";
submitButton.Text = "Login";
submitButton.Click += new EventHandler(_button_Click);
this.Controls.Add(userLabel);
this.Controls.Add(userTextBox);
this.Controls.Add(passTextBox);
this.Controls.Add(userValidator);
this.Controls.Add(passLabel);
this.Controls.Add(submitButton);
}

protected override void Render(HtmlTextWriter writer)
{
AddAttributesToRender(writer);
writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding,"1", false);
writer.RenderBeginTag(HtmlTextWriterTag.Table);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
userLabel.RenderControl(writer);
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Td);
userTextBox.RenderControl(writer);
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Td);
userValidator.RenderControl(writer);
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
passLabel.RenderControl(writer);
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Td);
passTextBox.RenderControl(writer);
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.AddAttribute(HtmlTextWriterAttribute.Colspan,"2", false);
writer.AddAttribute(HtmlTextWriterAttribute.Align,"right", false);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
submitButton.RenderControl(writer);
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.Write(" ");
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
}
}
#endregion

#region public class
// This class inherits from System.Web.UI.WebControls.WebControl and implements interface INamingContainer
public class CompositeControl : System.Web.UI.WebControls.WebControl, INamingContainer
{ 
}
#endregion
}

Now all you need to build the above class library and add the reference to your WebApplication.

Simply insert the Register Tag as:

<%@ Register TagPrefix="vik" Namespace="Controls" Assembly="Control" %>
<vik:comp runat="server" id="Comp1"></vik:comp>
private void Comp1_Submit(object sender, System.EventArgs e)<P>{</P><P>Response.Write(Comp1.userName);</P><P>Response.Write(Comp1.Password);</P><P>}</P>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralJust what I'm looking for Pin
musacj31-Jan-07 6:28
musacj31-Jan-07 6:28 
QuestionWhat is new? Pin
Joe Sonderegger22-May-06 2:52
Joe Sonderegger22-May-06 2:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.