Asp.net advanced tutorial (5): Practice (medium)

Source: Internet
Author: User

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 = '000000' 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 starts -------------------------->
<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-side vertical bars ---------------------->
<TD bgcolor = '#0097c0 'width = '1'>

</TD>
<! ------------------- Right-side vertical bars ---------------------->
</Tr>

<! ------------------- Horizontal line ------------------->
<Tr>
<TD colspan = 3 Height = 1 bgcolor = #0097c0> Height = 1> </TD>
</Tr>
<! ------------------- Horizontal line --------------->
</Table>

<My: Bottom id = "mybottom" runat = "server"> </My: bottom>
</Form>
</Body>

<! -------------------- End of file ---------------->

How about it? It looks familiar. Except for the first two sentences and the webcontrol with runat = server in the following webform, is it the same as the normal HTML? Note that codebehind = "register. CS "? It specifies that the code file after this page is register. CS, a mechanism provided by Asp.net, which can hide the business logic. in the CS file with the same name as aspx, this CS file is first compiled during runtime, which not only improves the running efficiency, but also hides the code, this avoids the source code leakage caused by system vulnerabilities in ASP. So what is the code file containing the business logic? The following is the file:

Namespace BBS
{
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data. SQL;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using bbs. uctrl;
// Using BBS. myclass;
Using myownclass;

/// <Summary>
/// Summary description for register.
/// </Summary>
Public class register: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. Button btnsubmit;
Protected system. Web. UI. webcontrols. textbox txthomepage;
Protected system. Web. UI. webcontrols. textbox txtemail;
Protected system. Web. UI. webcontrols. comparevalidator compassword;
Protected system. Web. UI. webcontrols. textbox txtpassword1;
Protected system. Web. UI. webcontrols. regularexpressionvalidator
Regularexpressionvalidator1;
Protected system. Web. UI. webcontrols. requiredfieldvalidator
Requiredfieldvalidator1;
Protected system. Web. UI. webcontrols. textbox txtpassword;
Protected system. Web. UI. webcontrols. customvalidator cususername;
Protected system. Web. UI. webcontrols. regularexpressionvalidator regusername;
Protected system. Web. UI. webcontrols. requiredfieldvalidator requsername;

Protected system. Web. UI. webcontrols. Label lblmessage;
Protected system. Web. UI. webcontrols. textbox txtusername;
Public myhead myhead1;

// Constructor
Public Register ()
{
Page. init + = new system. eventhandler (page_init );
}

Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
//
// Evals true first time browser hits the page
//
}
}

Protected void page_init (Object sender, eventargs E)
{
//
// Codegen: This call is required by the ASP + windows Form Designer.
//
Initializecomponent ();
This. myhead1.position = 2;
}

/// <Summary>
/// Required method for designer support-do not modify
/// The contents of this method with the code editor.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );
}

// Monitor whether the user exists
Public bool validuser (Object sender, string value)
{
Bbsuser myuser = new bbsuser ();
Bool bexists;
Try
{
Bexists = myuser.getuser(this.txt username. Text );
}
Catch (exception e) // if an exception occurs
{
# If debug
Response. Write (E. Message );
Return false;
# Endif
Server. Transfer ("error. aspx ");

}

Return! Bexists;
}

// Click the submit button
Public void onsubmit (Object sender, eventargs E)
{
If (page. isvalid)
{
// Data warehouse receiving
Try
{
Bbsuser myuser = new bbsuser ();
If (! Myuser. getuser (txtusername. Text ))
{
Myuser. createuser (bbsuser. createtype. Create, txtusername. Text,
Txtpassword. Text,
Txtemail. Text, txthomepage. Text ,"");
}
}
Catch (exception exp)
{
# If debug
Response. Write ("exception:" + exp. Message );
Return;
# Endif // debug
Server. Transfer ("error. aspx ");
}
}
}

}

}

What? Or are you familiar with it? That's right. Isn't that similar to the class I defined in the front? Yes. Originally, Asp.net regards this page as an object. Pay attention to the line of code defined by the class: public class register: system. web. UI. page, you can understand before, is to define a register object, that: system after the number. web. UI. what does page mean? It indicates that the regsiter class is system. web. UI. the derived class (subclass) of the page class, that is, the register class inherits system in addition to its own member variables, attributes, and methods. web. UI. all public or protected member variables, attributes, and methods of the page class. With this understanding, you can sit down and study the Code with peace of mind and prepare for the following content on form verification.

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.