Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Composite Login Control

0.00/5 (No votes)
22 May 2006 1  
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

public string userName

{

get

{

EnsureChildControls();

return userTextBox.Text;

}

set

{

EnsureChildControls();

userTextBox.Text = value;

}

}

public string Password

{

get

{

EnsureChildControls();

return passTextBox.Text;

}

set

{

EnsureChildControls();

passTextBox.Text = value;

}

}

[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)

{

Response.Write(Comp1.userName);

Response.Write(Comp1.Password);

}

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