Programming with the ASP.net control

Source: Internet
Author: User
Asp.net| Program | controls | The basic components of the ASP.net architecture when designing asp.net controls. Essentially, ASP. NET is a. Net
A class in the framework that can map a control declaration on a asp.net page. And then, those classes are based on their
property to create HTML for the control. Because the object of the class is compiled with the page at run time, it opens
Senders can access those objects in an object-oriented manner, such as reading and changing their properties, calling his
Our methods and handling server-side events, etc. In traditional ASP programming, the VBScript code of the service side
and HTML tags are mixed together and can easily cause confusion. And in asp.net programming, because of the ability to
Access objects in an object-oriented manner, so you can create better structured, more readable code.

1, ASP. NET, server controls in
Asp. NET provides a number of different server controls that can be grouped into the following categories:
<1> HTML server Controls (HTML servers control):
These controls are classes that contain standard HTML tags, which are declared by the runat= "server" attribute.
In addition to the runat= "server" attribute, other declarations of the control are unchanged, but by representing the object of the control,
You can access, read, and change the properties of a control in your code. For example, the runat= "server" attribute is supported
3 marks are <a>, <select> and <input>.

<2> asp.net Web Form Controls:
This is another set of controls that replicate the functionality of basic HTML tags (such as <input>, <a>, <select>, <button>, and so on).
However, the properties and methods in this set of controls are consistent, and the names of the properties and methods have a certain meaning, so
Developers can easily declare and access them. HyperLink, ListBox, and button are all such controls.

<3> asp.net list Controls (asp.net listing control):
This set of controls has consistent naming conventions and extends the properties of the basic Web Form control. They can be associated with
Data sources are bound (such as databases, XML files, and arrays) and can be used in a variety of ways, such as
Lists, tables, grids, and so on) display data.

<4> asp.net templated Controls (ASP. NET template control): This set of controls is the data source for each
Data items and repeating templates. They allow you to use different templates to display records, edit records, and emphasize important
Records, and can also support paging.

<5> asp.net Rich Controls:
By setting some of the properties of such controls, you can allow such advanced controls to generate HTML and JavaScript code.
The outstanding representatives of such controls are the calendar controls, which are discussed in detail in the content of this chapter.

<6> asp.net Validation Controls
Declaring such controls on a page can easily be based on certain criteria or user-defined rules, such as entering
It cannot be empty, input must be numeric, the number entered must be greater than a value, and so on, to validate the related input.
2. Create a server control programmatically
With ASP.net, you can create an instance of an HTML server control, set the control with an object-oriented method
Property and add the control to the Controls collection of the page so that all work is done.
For example, the following code (the code in htmlservtest2.aspx) is the entire contents of the new ASPX page:
There is no declaration of the control, only the server-side Page_Load program, which dynamically creates a 5-line
Table of 4 columns, set the color and text of the table, and then display the table on the page:
<script runat= "Server" language= "C #" >
void Page_Load ()
{
Create a new HtmlTable object
HtmlTable table1 = new HtmlTable ();
HtmlTableRow Row;
HtmlTableCell cell;

Set the table ' s styles
Table1. Border = 1;
Table1. cellpadding = 3;
Table1. cellspacing = 3;
Table1. BorderColor = "Red";
for (int i=1; i<=5; i++)
{
Create a new row and set its background color
row = new HtmlTableRow ();
Row. bgcolor = (i%2==0?) Lightyellow ":" Lightcyan ");

for (int j=1; j<=4; j + +)
{
Create a cell and set its text
Cell = new HtmlTableCell ();
Cell. InnerHtml = "Row:" + i.tostring () +
"<br>cell:" +j.tostring ();

Add the cell to the current row
Row. Cells.add (cell);
}

Add the row to the table
Table1. Rows.Add (row);
}


Add the table to page
PAGE.CONTROLS.ADD (table1);

}
</script>
There are two nested loops in the above code: The outer loop creates the line, and then goes into the inner loop, and the inner layer
Loops Create data cells and adds the data units to the cells collection of rows. When the inner loop ends at once,
Rows with data cells are added to the table's Rows collection. The final step is to add the table to the controls of the page
Collection. The results of the execution are shown in Figure 4-2.
From this table you create, you can see how many of the child controls (data cells and rows) are added to the parent's collection
(Rows and controls). This mechanism is also suitable for handling other server controls.


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.