Asp.net User Controls add custom events

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.