This article has shared the ASP.net control the use method, for everybody reference, the concrete content is as follows
1.asp.net page Insert C # code
Prior declaration:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
Introduction format: <%%>
Variables can be: <%=val%>
2. server controls and HTML controls
server controls:
-ASP. NET server controls are objects on server-side asp.net Web pages that, when users request ASP.net Web pages through a browser, run on the server and render HTML markup to the client.
-On the ASP.net page, the server control behaves as a token, such as <asp:textbox.../>. These tags are not standard HTML elements, so if they appear on a Web page, the browser will not understand them, however, when a ASP.net page is requested from a Web server, those tags are dynamically converted to HTML elements.
-with ASP.net server controls, you can significantly reduce the amount of code you need to write to develop Web applications, improve development efficiency, and Web application performance.
HTML control
-By default, ASP. NET files as text, and cannot be referenced in server-side code, and can only be controlled by scripting languages such as JavaScript and VBScript.
-By default, ASP. NET file is considered to be a token passed to the browser, processed as text and cannot be referenced in server-side code.
-To make these elements accessible programmatically, you can add runat= "Server" properties to indicate that HTML elements should be processed as server controls so that you can programmatically reference them using server-based code.
-HTML elements that have the attribute runat= "Server" added are converted to HTML server controls. The HTML server control is converted to an HTML element by removing the runat= "server" attribute from the control tag.
Summary:
There seems to be some overlap between standard controls and HTML controls, but HTML controls have much less functionality than controls in standard categories. In general, the true server controls in the standard category provide more functionality. But there is a cost to this function. Because they increase complexity, it takes a little more time to process server controls. However, on most Web sites, this difference may not be noticed. HTML controls provide slightly better performance only in a high traffic web site and with many controls on the page.
3. Control Add Event/event bindings
(1) in the ASP.net page, when declaring a control, specify the event handler for the control's event.
To add an OnClick event to a button:
Foreground aspx
<asp:button id= "Button1" runat= "Server" text= "button" onclick= "Myclick"/>
Background. CS Code
protected void Myclick (object sender, EventArgs e)
{
Response.Write ("<script>alert") SSS > ");
}
You can double-click the control to add events directly.
(2) If the control is created dynamically, you need to write code to dynamically bind the event to a method, for example:
Button mybtn = New button ("Button1");
Mybtn.text = "Submit";
Mybtn.click + = new System.EventHandler (ButtonClick);
4. Properties of the control
• Set the color of the control
• Set the font of the control
5. Validation controls
Asp. NET provides 6 validation controls, 5 controls are used to perform actual validation, and the last control ValidationSummary is used to provide feedback to the user about the errors that appear in the page.
RequiredFieldValidator: Verify a required field
CompareValidator: Compares user input to a constant value or to the values of another control or a specific data type (using comparison operators of less than, equal to, or greater than)
The Rangevalidator:rangevalidator control can be used to determine whether the value entered by a user is within a specific range
RegularExpressionValidator: It verifies that the format of the user input field is legitimate according to the regular expression, such as email, ID, phone number, etc.
CustomValidator: Checks user input with the validation logic that you write. This class of validation can check for values that are derived at run time
ValidationSummary: The control does not perform validation, but the control summarizes the validation error information for all validation controls on this page into a single list and displays the
Two common controls:
RangeValidator control
The RangeValidator control allows you to check whether a value falls within a specific range. This control can examine data types such as String, number, date, and currency. In addition to the common properties above, there are several other important properties of the RangeValidator control
MinimumValue This property to determine the minimum acceptable value. For example, when you examine an integer between 1 and 10, set the property to 1
MaximumValue This property to determine the maximum acceptable value. For example, when you examine an integer between 1 and 10, set the property to 10
Type This property determines the data type that the validation control checks. can be set to String, Integer, Double, date, or currency to check their data types
Usage Summary:
<form id= "Form1" runat= "Server" > <div> name: <asp:textbox id= "Txtrequiredfield" runat= "Server" ></a Sp:textbox> <%--requiredfieldvalidator: Verify a required field--%> <asp:requiredfieldvalidator ID= " RequiredFieldValidator1 "runat=" Server "errormessage=" name cannot be empty! "Controltovalidate=" Txtrequiredfield "></asp:RequiredFieldValidator> <br/> Age: <asp:textbox id=" t The Xtrange "runat=" server ></asp:TextBox> <%--rangevalidator:rangevalidator control can be used to determine whether the value entered by a user is within a specific range--% > <asp:rangevalidator id= "RangeValidator1" runat= "errormessage=" age is not within the prescribed range!
"Controltovalidate=" Txtrange "maximumvalue=" minimumvalue= "0" type= "Integer" ></asp:RangeValidator> <br/> Password: <asp:textbox id= "Txtpassword" runat= "server" ></asp:TextBox> <br/> Password Confirmation: <asp:t Extbox id= "txtpasswordconfirm" runat= "Server" ></asp:TextBox> <%-- CompareValidator: Compares user input to a constant value or a value of another control or a specific data type (using a comparison operator less than, equal to, or greater than)--%> <asP:comparevalidator id= "CompareValidator1" runat= "server" errormessage= "password before and after input, inconsistent!" "Controltocompare=" Txtpassword "controltovalidate=" txtpasswordconfirm "></asp:CompareValidator> <br/ > e-mail: <asp:textbox id= "Txtmail" runat= "Server" ></asp:TextBox> <%-- RegularExpressionValidator: It verifies that the format of the user input field is legitimate according to the regular expression, such as email, ID, phone number--%> <asp:regularexpressionvalidator ID = "RegularExpressionValidator1" runat= Server "errormessage=" mailbox format does not match! "Controltovalidate=" Txtmail "validationexpression=" \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) * "></asp:RegularExpressionValidator> <br/> <%--validationsummary: The control does not perform validation,
However, the control summarizes the validation error information for all validation controls on this page as a list and displays the--%> <asp:validationsummary id= "ValidationSummary1" runat= "Server" centrally. headertext= "Validation information rollup"/> <br/> <asp:button id= "btnsubmit" Server "runat=" Submit "text=" onclick=
"/> </div> </form>
6. Navigation controls
asp.net 4 provides 3 useful navigation controls: SiteMapPath, TreeView, and menu.
1.SiteMapPath: This Web control provides a bread bar (breadcrumb), which is a line of text that shows where the user is currently in the site structure. For example, in an online bookstore, if a user browses to the Visual
C + +, the bread Bar may resemble "Home-> computer-> programming class->visual
C + +, where each part (such as the home page, computer, and so on) is displayed as a link back to the previous section. Bread bars enable users to quickly view the current location in a Web site and navigate up the logical hierarchy.
2.Menu: This Web control provides a hierarchical view of the structure of the Web site. For the school's web site, the top-level menu will contain the main categories (such as school introductions, organization settings, news, etc.), and each menu item can contain its own submenu, displaying its own subcategories.
3.TreeView: The tree view provides the same data as the menu, and the only difference is how the data is displayed. The tree view is displayed as an expandable or collapsible tree, and a menu is made up of both menu items and submenus.
Site Map Files:
The hierarchical relationship of the site is the same as the hierarchy in the HTML tag.
<?xml version= "1.0" encoding= "Utf-8"?> <sitemap xmlns=
"http://schemas.microsoft.com/AspNet/" sitemap-file-1.0 ">
<sitemapnode url=" default.aspx "title=" Home "description=" ">
<sitemapnode URL = "Default2.aspx" title= "second page" description= ""/>
<sitemapnode url= "default3.aspx" title= "third page" description= "" />
</siteMapNode>
</siteMap>
To reference a site map in an ASPX file:
<form id= "Form1" runat= "Server" > <%--using sitemappath--%> <asp:sitemappath id=
" SiteMapPath1 "runat=" Server ></asp:SiteMapPath>
<!--the SiteMapDataSource control here is provided for the TreeView and Menu use- >
<asp:sitemapdatasource id= "SiteMapDataSource1" runat= "server"/> <%--using treeview--%>
<asp:treeview id= "TreeView1" runat= "Server" datasourceid= "SiteMapDataSource1"/> <%--using-menu--
%>
<asp:menu id= "Menu2" runat= "Server" datasourceid= "SiteMapDataSource1"/> <%--using
A Horizontal menu--%>
<asp:menu id= "Menu1" runat= "Server" datasourceid= "SiteMapDataSource1
" orientation= "Horizontal" staticdisplaylevels= "2"/>
</form>
7. User Control
For user control, in asp.net development, is a very important content, he can be a large program simplification of the use of the process. (is actually multiple controls?) Include HTML controls and server controls? , and you can customize the properties)
Make a user control. Solution Manager-Right-"Add New Item-" Web user control. One more. ascx file in Solution Manager, this file is a control.
1. Add control Code: Drag into several server spaces and double-click button to enter the background:
<asp:label id= "ColumnName" runat= "Server" ></asp:Label>
<asp:textbox id= "Condition" runat= "Server" ></asp:TextBox>
<asp:button id= "Search" runat= "server" text= "Searching"/>
2. Add control events and properties:
public string Labletext {get; set;} Prompts the user to enter what query criteria
public string connectionString {get; set;} Connecting to a database
Public GridView Resultgridview {get; set;} The GridView control to populate
public string TableName {get; set;} To query the data table name in the database
public string Columncondition {get; set;} According to which one to query
public string ErrorMessage {get; set;} Error message
To use a custom control:
Drag the ascx file directly to the page we want to use.
<form id= "Form1" runat= "Server" >
<div>
<uc1:webusercontrol runat= "Server" id= Webusercontrol "/>
</div>
</form>
You can view the properties that you added:
3 wonderful topics to be attached:
Asp. NET Control Usage Manual
Asp. NET data-bound controls using totals
Asp. NET controls use totals
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.