Add events to custom Web controls (front and back-end code)

Source: Internet
Author: User

------------ MyRegister1.ascx front-end code -----------
Copy codeThe Code is as follows:
<Script src = "js/Jquery1.7.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
Function Register (){
If ($ ('# myregisterpolictxtusername'). val () = ''){
$ ('# Spanusername'). text ('enter your username ');
Return false;
}
If ($ ('# myregister=txtpwd'). val () = ''){
$ ('# Spanpwd'). text ('Enter the password ');
Return false;
}
If ($ ('# myregister=txtpwd1'). val () = ''){
$ ('# SpanPwd1'). text ('Enter the password ');
Return false;
}
If ($ ('# myregister=txtpwd1'). val ()! = $ ('# Myregister=txtpwd'). val ()){
$ ('# SpanPwd1'). text ('two passwords must be consistent ');
Return false;
}
If ($ ('# myregister=txtemail'). val () = ''){
$ ('# SpanEmail'). text ('enter your mailbox ');
Return false;
}
Return true;
}
</Script>
<Table>
<Tr>
<Td> User name: </td>
<Td>
<Asp: TextBox ID = "txtUserName" runat = "server"> </asp: TextBox> </td>
<Td> <span id = "spanUserName"> </span> </td>
</Tr>
<Tr>
<Td> password: </td>
<Td>
<Asp: TextBox ID = "txtPwd" runat = "server"> </asp: TextBox> </td>
<Td> <span id = "spanPwd"> </span> </td>
</Tr>
<Tr>
<Td> Confirm Password: </td>
<Td>
<Asp: TextBox ID = "txtPwd1" runat = "server"> </asp: TextBox> </td>
<Td> <span id = "spanPwd1"> </span> </td>
</Tr>
<Tr>
<Td> Email: </td>
<Td>
<Asp: TextBox ID = "txtEmail" runat = "server"> </asp: TextBox> </td>
<Td> <span id = "spanEamil"> </span> </td>
</Tr>
<Tr>
<Td> </td>
<Td>
<Asp: Button ID = "btnRegister" runat = "server" Text = "register"
Onclick = "btnRegister_Click" OnClientClick = "return Register ()"/> </td>
<Td>
<Asp: Label ID = "Label1" runat = "server" Text = ""> </asp: Label>
</Td>
</Tr>
</Table>

------------ MyRegister1.ascx background code ------------
Copy codeThe Code is as follows:
Public partial class myRegister: System. Web. UI. UserControl
{
Public event MyRegeitserDelegate On_MyRegister;
Protected void Page_Load (object sender, EventArgs e)
{
}
Protected void btnRegister_Click (object sender, EventArgs e)
{
If (On_MyRegister! = Null)
{
MyRegister. userName = txtUserName. Text;
MyRegister. pwd = txtPwd. Text;
MyRegister. email = txtEmail. Text;
On_MyRegister ();
If (MyRegister. isBool = true)
{
Label1.Text = "successfully registered ";
}
Else
{Label1.Text = "registration failed ";}
}
}
}
Public class MyRegister
{
Public static string userName {set; get ;}
Public static string pwd {set; get ;}
Public static string email {set; get ;}
Public static bool isBool {set; get ;}
}
Public delegate void MyRegeitserDelegate ();

--------- No parameter for the WebForm event. aspx front-end code --------------
Copy codeThe Code is as follows:
<Uc1: myRegister ID = "myRegister1" runat = "server"/>

--------- No parameter for the WebForm event. aspx background code ---------------
Copy codeThe Code is as follows:
Public partial class WebForm event without parameter: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
This. myRegister1.On _ MyRegister + = new MyRegeitserDelegate (myregisterpolicon_myregister );
}
Void myregisterpolicon_myregister ()
{
MyRegister. isBool = writer (MyRegister. userName, MyRegister. pwd, MyRegister. email );
}
Private bool writer (string userName, string pwd, string email)
{
Using (Stream stream = File. Open (@ "E: \ new.txt", FileMode. Append, FileAccess. Write ))
{
Using (StreamWriter writer = new StreamWriter (stream ))
{
Writer. WriteLine ("userName: {0}, password: {1}, email: {2}", userName, pwd, email );
}
}
Return true;
}
}

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.