The use of user controls has such characteristics. That is, when a user control is required to implement specific functions, it can be dragged and dropped randomly on the page of the entire website. However, its functions are relatively fixed, that is, the code in the ascx file is written to death. Once other functions are implemented, the entire user control must be redone. Here we will introduce a method that requires more reusable use of user controls.
Front-end code:
Is a simple logon Control
<% @ Control Language = "C #" AutoEventWireup = "true" CodeFile = "LoginControl. ascx. cs" Inherits = "LoginControl" %>
<Table border = "1">
<Tr>
<Td colspan = "3" style = "background-color: # 0099ff; text-align: center">
<Strong> User Logon </strong> </td>
</Tr>
<Tr>
<Td style = "width: 100px">
Username: </td>
<Td colspan = "2">
<Asp: TextBox ID = "txtName" runat = "server" ValidationGroup = "group1"> </asp: TextBox> </td>
</Tr>
<Tr>
<Td style = "width: 100px; height: 30px">
Password: </td>
<Td colspan = "2" style = "height: 30px">
<Asp: TextBox ID = "txtPassword" runat = "server" TextMode = "Password"> </asp: TextBox> </td>
</Tr>
<Tr>
<Td colspan = "3" style = "text-align: center">
<Asp: RequiredFieldValidator ID = "RequiredFieldValidator1" runat = "server" ControlToValidate = "txtName"
ErrorMessage = "username is required" ValidationGroup = "group1"> </asp: RequiredFieldValidator> </td>
</Tr>
<Tr>
<Td style = "width: 100px">
<Asp: Button ID = "Button3" runat = "server" Text = "Button"/> </td>
<Td style = "width: 100px; text-align: center">
<Asp: Button ID = "Button1" runat = "server" OnClick = "button#click" Text = "login" ValidationGroup = "group1"
Width = "56px"/> </td>
<Td style = "width: 100px; text-align: center">
<Asp: Button ID = "Button2" runat = "server" Text = "logout" Width = "54px"/> </td>
</Tr>
</Table>
User Control background code
Public event EventHandler Authenticate;
Protected void Page_Load (object sender, EventArgs e)
{
}
Public void button#click (object sender, EventArgs e)
{
If (Authenticate! = Null)
{
Authenticate (this, new EventArgs (); // If a custom event is added, the button will be executed based on the specified event instead of the default event. On the contrary, default events are executed.
}
Else
{
String connectString = "Server =.; DataBase = Test; uid = sa; pwd = cosecose ";
String selectString = "Select * from userInfo where Name = '" + txtName. text. toString () + "'and Password ='" + txtPassword. text. toString () + "'";
SqlConnection conn = new SqlConnection (connectString );
Conn. Open ();
SqlCommand cmd = new SqlCommand (selectString, conn );
SqlDataAdapter sda = new SqlDataAdapter ();
Sda. SelectCommand = cmd;
DataTable dt = new DataTable ();
Sda. Fill (dt );
If (dt. Rows. Count = 0)
{
Response. Write ("<script> window. alert ('incorrect password or username! ') </Script> ");
}
Else
{
Response. Write ("<script> window. alert ('Congratulations, logon successful! ') </Script> ");
}
Conn. Close ();
}
}
Homepage code
Protected void Page_Load (object sender, EventArgs e)
{
Button btn = (Button) LoginControl1.FindControl ("Button2 ");
Btn. Click + = new EventHandler (Show );
// Provide the first method. Use the findcontrol method to locate the control and add the control events.
LoginControl1.Authenticate + = new EventHandler (Show );
// Provides the second method to bind the Custom Event directly.
}
Protected void Show (object sender, EventArgs e)
{
FormsAuthentication. SignOut ();
Response. Write ("<script> window. alert ('the user has successfully exited! ') </Script> ");
Author Zheng wenliang