Registration exercise:
Verification code generation:
Public void ProcessRequest (HttpContext context)
{
String str = ""; Random r = new Random ();
For (int I = 0; I <= 4; I ++)
{
Int j = r. Next (10 );
Str = str + j;
}
Context. Session ["YanZheng"] = str;
Context. Response. ContentType = "image/JPEG ";
Using (System. Drawing. Bitmap bitmap = new System. Drawing. Bitmap (55,20 ))
{
Using (System. Drawing. Graphics g = System. Drawing. Graphics. FromImage (bitmap ))
{
G. DrawString (str, new System. Drawing. Font ("? Body? ", 14), System. Drawing. Brushes. Red, 0, 0 );
}
Bitmap. Save (context. Response. OutputStream, System. Drawing. Imaging. ImageFormat. Jpeg );
}
}
The Regular Expression in C # is similar to that in js.
Three controls:
The HTML control and Asp. Net render the control as a normal string to the browser, without verifying the correctness or processing on the server. (If an element is wrong, no error will be reported)
The Asp.net server-side control is highly encapsulated in Asp.net. It is easy to use and runs on the server side. You can use the C # code on the server side for operations and render it to the client as an HTML control.
Runat = server HTML control. Adding runat = server on the basis of the HTML control is also running on the server side. You can also use the C # code for operations and render it to the client, which is not as highly encapsulated as the Asp.net server Control, most exposed attributes are common HTML attributes. Compared with the Asp.net server control, when the control needs to be operated on the server side, if the control is not encapsulated by the Asp.net Server Control, it is very convenient to use the html control of ruant = Server. The html control of runat = Server also processes the virtual path, id-> ClientID, therefore, it may also be used in virtual paths and usercontrol (which can be used to operate hyperlinks, <tr> <td>
After a non-control such as div is added with runat = server, it will become a control of the HtmlControls. HtmlGenericControl type.
Some attributes are encapsulated. to operate other unencapsulated attributes such as the "class" css style class, you can use div1.Attributes ["class"] = "error";. In this way, the style of the div is error.
A-à HtmlAnchor; form-à HtmlForm; head-à HtmlHead;
Input à HtmlInputButton HTmlInpuCheckBox HtmlInputText, meta à HtmlMeat; HtmlTable, HtmlTableRow HtmlTableCell; HtmlTitle. The corresponding type of HtmlGenericControl is not separately encapsulated (such as div. Use the Attributes attribute operation as the encapsulated attribute.
Verification control:
Cannot be empty validation: InitialValue = "please fill in the name" to set the initial value, when the control is this value, it is considered empty.
The IsValid attribute in the page is used to determine whether all Validator checks on the page have passed. It is True only when all the checks have passed. When does the Validator Server check on the page report an error, the business code in the server-side method will also be executed. Therefore, if the code is not executed when the data verification fails, you need to determine the IsValid value.
<Span style = "display: none"> no space occupied by me </span>
<Span style = "visibility: hidden"> I cannot be seen </span>.
The verification control has the display attribute to determine how to display the error message. There are three values: static: when there is no error message, the visiblity style of the control is hidden. Dynamic: when there is no error message, use the display style as none to hide it. Another one is None.
Both the client and server are verified at the same time.
You can group a page by setting ValidationGroup. Sometimes there are two forms on a page. To prevent them from interfering with each other, you can group them. Similar to radiobutton.
If you set the CausesValidation attribute of the button control to false, clicking this button does not trigger verification.
Range verification control: RangeValidator. Date, String, Double, int, and other types of data can be selected, range can be programmed to dynamically set.
Regular Expression verification: You can customize the verification expression, for example, QQ number (5-11 digits) \ d {5, 11}
Personal Description: 10 to 50 characters \ w {10, 50}
Each verification control has two attributes: ErrorMessage and Text. ErrorMessage is used to display the values in the ValidationSummary control. Text is used to display the values above the verification control. If Text is empty, the error message is displayed on both the summary control and verification control.
Master page
If you reference an image on the master page, the path may be incorrect. You must use the image control on the server.
Set the path to relative path and Relative Application Path. Example: ImageUrl = "~ /Images/banner.jpg ";
You can also use the HTML control src = "~ /Images/banner.jpg ";
If you do not want to use server-side controls to consume too much resources, you can also use ResolveClientUrl ("~ /Images/banner.jpg) method combined with "html page using C # background code:
<% = ResolveClientUrl ("~ /Images/banner.jpg ") %> dynamic conversion path.
<% @ Page Title = "Homepage" Language = "C #" MasterPageFile = "~ /Master/MyMaster1.Master "AutoEventWireup =" true "CodeBehind =" WebForm1.aspx. cs "Inherits =" register exercise. WebForm1 "%>
If the master Page is used, you need to change the Title: you only need to change the top Page Title = "xx.
The Server-side control on the master page must be placed in the form of runat = Server.
To operate the server control on the master page on a specific page, use the following method:
Button B = (Button) this. Master. FindControl ("btnMaster ");
B. Visible = false;
Data Binding controls
Use WebApplication from data binding, otherwise it will be a lot of trouble.
Data Binding is divided into two parts: data source and data binding control. The data binding control obtains data through the data source and isolates the data provider and data user through the data source, the data binding control modifies the data through the data source.
Data sources include SqlDataSource, AccessDataSource, ObjectDataSource, LinqDataSource, EntityDataSource, and XmlDataSource. Because most projects do not directly connect to the database on pages (because they violate the basic layering principles), SqlDataSource, accessDataSource is not used, and LinqDataSource and EntityDataSource are only used in ultra-challenging projects using Linq.
XmlDataSource can be used only when xml data is processed. ObjectDataSource is the most widely used data source in web development and can easily switch databases.
Data Binding controls include list data binding controls (DropDownList, RadioButtonList, ListBox, CheckBoxList, BulletedList, and so on) and complex controls (DataGrid (useless after 2.0 ), gridView, DetailsView, FormView, ListView, Repeater, DataList) GridView are all ListView subsets.
Repeater is the most lightweight component. Most frequently used on the Internet front-end, ListView is ASP. added controls in Net3.5. ListView is a big unification of controls such as GridView, DetailsView, FormView, Repeater, and DataList. All the advantages of those controls are ListView, and ListView is enabled, and other controls are used, therefore, data binding controls mainly refer to list data binding controls, Repeater, and ListView. FormView and GridView are used in the project.