Asp.net advanced tutorial (5) --- practice (combined with forum user operations to discuss asp.net Form Verification) (medium)
Now we have to make preparations. Let's take a look at the implementation of user registration. As mentioned above, asp.net can separate the business logic from the html code. Let's take a look at how it is implemented. The following file is the page part of user registration, the prototype is based on the style of my site:
<% @ Page language = "c #" Codebehind = "Register. cs" AutoEventWireup = "false" Inherits = "bbs. Register" %>
<% @ Register Tagprefix = "My" Namespace = "bbs. uctrl" %>
<Html> <TITLE> New User Registration </TITLE>
<Meta name = vs_targetSchema content = "HTML 4.0">
<Link rel = "stylesheet" href = "images/style.css">
<META http-equiv = Content-Type content = "text/html; charset = gb2312">
<Meta name = "GENERATOR" Content = "Microsoft Visual Studio 7.0">
<Meta name = "CODE_LANGUAGE" Content = "C #"> <Script language = javascript>
Function OnPreview ()
{
DivPreview. innerHTML = Form1.txtSignature. value;
}
</Script>
<Body>
<Form method = "post" runat = "server" ID = Form1>
<My: MyHead id = "myHead1" runat = "server"> </MY: MYHEAD>
<! ---------------------- The outer table forms a border -------------------------------------->
<Table width = 722 border = 0 cellspacing = 0
Cellpadding = 0 align = center>
<TBODY>
<Tr>
<! ------------------- Left-side vertical bars -------------------------------------------->
<Td bgcolor = #0097c0 width = 1>
</Td>
<Td width = 720 align = middle> <br>
<! ------------------- Left-side vertical bars -------------------------------------------->
<! -------------- New User Registration starts ------------------------------------------------>
<Table width = 600 align = center border = 0 cellpadding = 4 cellspacing = 1
Id = "tblRegister" class = cn bgcolor = #000000 runat = server>
<TBODY>
<Tr bgcolor = # ffffff>
<Td colspan = 3>
<P align = center> New User Registration </p>
</Td>
</Tr>
<! -------------- User name start ------------------------------------------------------>
<Tr bgcolor = # ffffff>
<Td width = 60>
User Name
</Td>
<Td width = 300>
<Asp: TextBox id = "txtUserName" maxlength = 20 columns = 20 runat = "server"> </asp: TextBox>
<Font color = red> * </font>
</Td>
<Td width = 240>
User pen name, 4-20 characters
<Asp: requiredfieldvalidator id = "reqUserName" display = Dynamic
Controltovalidate = "txtUserName" runat = Server>
Cannot be blank!
</Asp: requiredfieldvalidator>
<Asp: regularexpressionvalidator id = "regUserName" display = Dynamic
Controltovalidate = "txtUserName" runat = Server validationexpression = "[^] {4, 20}">
Invalid user name!
</Asp: regularexpressionvalidator>
<Asp: customvalidator id = "cusUserName" controltovalidate = "txtUserName"
Onservervalidate = "ValidUser" display = Dynamic runat = Server>
This user already exists.
</Asp: customvalidator>
</Td>
</Tr>
<! -------------- End of user name ---------------------------------------------------->
<! -------------- User Password start -------------------------------------------------------->
<Tr bgcolor = # ffffff>
<Td width = 60>
Password
</Td>
<Td width = 300>
<Asp: TextBox id = "txtPassword" maxlength = 10 columns = 10
Textmode = Password runat = server> </asp: TextBox>
<Font color = red> * </font>
</Td>
<Td width = 240>
User Password, 4-10 Characters
<Asp: requiredfieldvalidator id = Requiredfieldvalidator1 display = Dynamic
Controltovalidate = "txtPassword" runat = Server>
Cannot be blank!
</Asp: requiredfieldvalidator>
<Asp: regularexpressionvalidator id = Regularexpressionvalidator1 display = Dynamic
Controltovalidate = "txtPassword" runat = Server validationexpression = "[^] {4, 10}">
Invalid password!
</Asp: regularexpressionvalidator>
</Td>
</Tr>
<! -------------- End of User Password ------------------------------------------------------>
<! -------------- Password verification starts -------------------------------------------------------->
<Tr bgcolor = # ffffff>
<Td width = 60>
Verify Password
</Td>
<Td width = 300>
<Asp: TextBox id = txtPassword1 maxlength = 10 columns = 10
Textmode = Password runat = server> </asp: TextBox>
<Font color = red> * </font>
</Td>
<Td width = 240>
Enter the password again.
<Asp: comparevalidator id = "comPassword" display = Dynamic
Controltocompare = "txtPassword" controltovalidate = "txtPassword1" runat = Server>
The two passwords are different!
</Asp: comparevalidator>
</Td>
</Tr>
<! -------------- Verification password ended -------------------------------------------------------->
<! -------------- Email starts -------------------------------------------------------->
<Tr bgcolor = # ffffff>
<Td width = 60>
Email
</Td>
<Td width = 300>
<Asp: TextBox id = "txtEmail" maxlength = 100 columns = 30 runat = server> </asp: TextBox>
</Td>
<Td width = 240>
Your email address. You can leave it empty, but do not leave it empty.
<Asp: regularexpressionvalidator id = "regEmail" display = Dynamic
Controltovalidate = "txtEmail" validationexpression = "[^] *" runat = Server>
Invalid character
</Asp: regularexpressionvalidator>
</Td>
</Tr>
<! -------------- Email ended -------------------------------------------------------->
<! -------------- Personal homepage ---------------------------------------------------------->
<Tr bgcolor = # ffffff>
<Td width = 60>
Personal Homepage
</Td>
<Td width = 300>
<Asp: TextBox id = "txtHomepage" maxlength = 150 columns = 30 runat = server> </asp: TextBox>
</Td>
<Td width = 240>
You can leave this field empty for your homepage.
<Asp: regularexpressionvalidator id = "regHomepage" display = Dynamic
Controltovalidate = "txtHomepage" validationexpression = "[^] *" runat = Server>
Invalid character.
</Asp: regularexpressionvalidator>
</Td>
</Tr>
<! -------------- Personal homepage ends -------------------------------------------------------->
<! -------------- Signature start -------------------------------------------------------->
<Tr bgcolor = # ffffff>
<Td width = 60>
Signature
</Td>
<Td width = 300>
<Asp: TextBox id = "txtSignature" maxlength = 150 columns = 30 rows = 6 textmode = MultiLine runat = server> </asp: TextBox>
</Td>
<Td width = 240 valign = top>
<Div id = "divPreview">
You can create your own signature, which must not exceed 255 characters and cannot use a script. <Br>
</Div> <br>
<Input type = button id = "btnPreview" value = "preview" onclick = "OnPreview ()"> <br>
<Asp: regularexpressionvalidator id = Regularexpressionvalidator2 display = Dynamic
Controltovalidate = "txtSignature" validationexpression = "[^] {0,255}" runat = Server>
Invalid characters or more than 255 characters are used.
</Asp: regularexpressionvalidator>
</Td>
</Tr>
<! -------------- Signature ended -------------------------------------------------------->
<Tr bgcolor = # ffffff>
<Td colspan = 3 align = center>
<Asp: button id = "btnSubmit" text = "OK" onclick = "OnSubmit" runat = Server> </asp: button>
</Td>
</Tr>
</Table> <br>
<! ---------------------------------- Registration of new users ends -------------------->
<! ------------------- Right vertical line ------------------------------------------------>
<Td bgcolor = #0097c0 width = 1>
</Td>
<! ------------------- Right vertical line ------------------------------------------------>
</Tr>
<! ------------------- Horizontal line -------------------------