Click here to Skip to main content
Licence 
First Posted 16 May 2006
Views 21,722
Bookmarked 21 times

Composite Login Control

By | 22 May 2006 | Article
This article shows the implementation of Composite Server Control to provide Login features
 
Part of The SQL Zone sponsored by
See Also

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

About the Author

vikas vohra

Web Developer

India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralJust what I'm looking for Pinmembermusacm6:28 31 Jan '07  
QuestionWhat is new? PinmemberNice Life2:52 22 May '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 22 May 2006
Article Copyright 2006 by vikas vohra
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid