Introduction
Here we will
discuss about user controls in Asp. net. Why we used user controls.
A user control is a kind of
composite control that works much like an ASP. NET Web page—you can add existing
Web server controls and markup to a user control, and define properties and
methods for the control. You can then embed them in ASP.NET Web pages, where
they act as a unit.
Create Login User Control
Now we are going to make a login Control which validates whether the entered text in the
control is valid or not. If there is any textbox empty then it will show a validation to fill the textbox values. It will
display an error which shows that something going wrong. It will also fire events
that if both the value of user id and password is correct then raise login
successfully, other wise it will raise event which shows login failed.
Step 1:
Firstly you have to start Visual Studio 2010 and take a Window Forms Control Library and set the controls on.
Take two label named as User Id
and Password
Take two textbox and a button on
window form and a error provider control.
Step 2:
Now we have to start to write the Code
for our Control:-
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Drawing;
using
System.Data;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
namespace
WindowsFormsControlLibrary1
{
public partial
class UserControl1
: UserControl
{
private System.Windows.Forms.StatusBar
stbMessage;
public delegate
void EventHandler(Object
sender, EventArgs e);
public event
EventHandler SuccessfullyLogin;
public event
EventHandler LoginFail;
//Constructor of the
class
public
UserControl1()
{
InitializeComponent();
}
// This is the very
simple Login Check Validation
// The Password mus be ... "secret" .....
private
bool LoginCheck(string
pName, string pPassword)
{
return pPassword.Equals("secret");
}
// Validate Login, in
any case call the LoginSuccess or
// LoginFailed event, which will notify the Application's
// Event Handlers.
private void
button1_Click(object sender,
EventArgs e)
{
// User Name
Validation
if
(txtnam.Text.Length == 0)
{
errorpro.SetError(txtnam,"Please
enter a user name");
stbMessage.Text = "Please enter
a user name";
return;
}
else
{
errorpro.SetError(txtnam,"");
stbMessage.Text = "";
}
// Password
Validation
if
(txtpw.Text.Length == 0)
{
errorpro.SetError(txtpw,"Please
enter a password");
stbMessage.Text = "Please enter
a password";
return;
}
else
{
errorpro.SetError(txtpw,"");
stbMessage.Text = "";
}
// Check
Password
if
(LoginCheck(txtnam.Text, txtpw.Text))
{
// If there
any Subscribers for the LoginSuccess
// Event, notify them ...
if
(SuccessfullyLogin != null)
{
SuccessfullyLogin(this,
new System.EventArgs());
}
}
else
{
// If there
any Subscribers for the LoginFailed
// Event, notify them ...
if (LoginFail != null)
{
LoginFail(this,
new System.EventArgs());
}
}
}
// Read-Write Property
for User Name Label
public
string LabelName
{
get
{
return lblname.Text;
}
set
{
lblname.Text = value;
}
}
// Read-Write
Property for User Name Password
public
string LabelPassword
{
get
{
return lbpwd.Text;
}
set
{
lbpwd.Text = value;
}
}
// Read-Write
Property for Login Button Text
public
string LoginButtonText
{
get
{
return btnlg.Text;
}
set
{
btnlg.Text = value;
}
}
// Read-Only
Property for User Name
[Browsable(false)]
public
string UserName
{
set
{
txtnam.Text = value;
}
}
// Read-Only
Property for Password
[Browsable(false)]
public
string Password {
set
{
txtpw.Text = value;
}
}
}
}
Step 4: Open New Window Application and Add a Reference Of .dll file which we created. Write the
code given below.
Code :
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
namespace
TestApp
{
public partial
class Form1 :
Form
{
[STAThread]
static
void Main()
{
Application.Run(new
TestApp());
}
// This Event is fired by the Login
Validation User Control
private void
LoginFailed(object sender, System.EventArgs
e)
{
MessageBox.Show("Login
falied ....", "Login Validation",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
// This Event is fired by the Login
Validation User Control|
private void
LoginSuccess(object sender, System.EventArgs
e)
{
MessageBox.Show("Login
success ....", "Login Validation",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
}
}
Step 5: Double click on the dll file.
Step 6: Test application and press F5 again.