Web controls
You may be worried that controls such as <asp: TextBox> represent a new set of controls that require your proficiency. However, they are not difficult to learn because they all have their counterparts in HTML. For example, for a simple text box, in HTML, you do this:
<Input type = "text" value = "Your Name"> </input>
The corresponding Web control is as follows:
<Asp: TextBox Text = "Your Name" runat = "server"/>
In both cases, you can immediately notice that the Web control is identified by the code "asp:" namespace, and uses a slash to end the element like in XML. You do not have to use XML format. You can also use HTML format to end the tag with a semicolon: </asp: TextBox>. However, you will find that the XML format is used in many code examples, and the code word count is also concise. A namespace must be used to identify the source of the TextBox Control. All standard Web controls are part of the ASP namespace. This is important when writing your own controls.
The TextBox Control has little advantage over the standard input box, but you should consider the following three input controls:
<Input type = "text"...>
<Input type = "password"...>
<Textarea rows = "5"...>
They are all used for HTML input, but they are not consistent. But is it simpler to use the following?
<Asp: TextBox runat = "server"...>
<Asp: TextBox TextBoxMode = "Password"...>
<Asp: TextBox Rows = "5"...>
As you can see, a simple control contains the functions of three HTML controls, which are easier to remember and encode.
ASP. NET carries five types of Web controls:
• Inherent controls corresponding to HTML
• Provides a data stream list control on the page.
• Rich controls that provide richer UI (User Interface) content and functions
• Validation controls for completing various Form Verification
• Encapsulate WML mobile controls for WAP Devices
The inherent server control is the same as the HTML control, but it is more reasonable and can provide a more consistent purpose. These controls include LinkButton, ImageButton, HyperLink, TextBox, CheckBox, and RadioButton) dropDownList, ListBox, Image, Label, Panel, Table, and TableRow) and TableCell ).
The List controls include Repeater, DataList, and DataGrid ). The list control also includes the RadioButtonList and CheckBoxList, which makes it easy to create a list of single quotes and check boxes.
The Rich control includes Calendar and AdRotator ). The Calendar control outputs pure HTML for low-level browsers and DHTML for advanced browsers (such as Internet Explorer 5.0 ). AdRotator outputs the image, which has built-in rotation code.
Validation controls include RequiredFieldValidator, Compare Validator, RangeValidator, RegularExpressionValidator, and CustomValidator) and ValidationSummary ). These controls provide a simple way for developers to establish validation in form processing.
Information about mobile controls has not yet been released, but it is certain that they help to construct a Web site that activates WAP.
Write New Controls
You are not limited to using the controls provided by these systems. Writing controls yourself is also quite simple. For example, if you want a control to encapsulate two text boxes (which may be an input field with the first name and last name), you can write the code as follows:
<Asp: Panel runat = "server">
<Asp: Textbox id = "txtFirstName" text = "First Name" runat = "server"/>
<Asp: Textbox id = "txtLastName" text = "Last Name" runat = "server"/>
</Asp: Panel>
You can save this code in the file Name. aspc (note the new extension) and treat it as a Web form control. Then, you can add the following content to the Web form:
<% @ Register TagName = "NameControl" TagPrefix = "Foo" Src = "Name. aspc" %>
<Form>
<Foo: NameControl runat = "Server"/>
</Form>
In this way, you can easily create reusable controls. This is really beautiful;-) You can also create controls in Visual Basic or C # directly, allow them to become subclasses of other controls, and present any output they require. Controls are identified by namespaces, so there should be no conflict between controls. In fact, controls can even use the same name as long as they are in different namespaces. You will feel that this makes ASP. NET Very extensible and enriches the programming environment. In fact, there is a large third-party market that provides rich controls.
Data bundle Control
One of the new Web controls is the data grid DataGrid, which is a built-in support control for displaying complete sets of data. To generate an HTML table from SQL-generated data, you only need to create an ADO + object and execute commands to obtain the data as a grid data source. For example, the following table 4 Code:
Table 4 DaveSGrid1.aspx
<% @ Import Namespace = "System. Data. SQL" %>
<Html>
<Script language = "VB" runat = "server">
Sub Page_Load (Sender As Object, E As EventArgs)
Dim myCommand As SQLCommand
MyCommand = New SQLCommand (explain select * from products ",
Login Server = localhost; Database = AdvWorks; UID = sa ")
DataGrid1.DataSource = myCommand. Execute
DataGrid1.DataBind
End Sub
</Script>
<Body>
<ASP: DataGrid id = "DataGrid1" runat = "server"/>
</Body>
</Html>
All you need to do is to bind the data to the data grid and generate a neat HTML table:
Data bundling is not limited to data from databases. You can also bind the data to hash tables, arrays, other server controls, and appropriate pages. If the default column is not suitable, you can also customize it to show the content you are interested in:
<Asp: DataGrid id = "DataGrid1"
AutoGenerateColumns = "false" runat = "server">
<Property name = "Columns">
<Asp: BoundColumn HeaderText = "Name" DataField = "ProductName"/>
<Asp: BoundColumn HeaderText = "Description"
DataField = "ProductDescription"/>
</Property>
</Asp: DataGrid>
Use the BoundColumn control to select a simple column and specify the title of the column and where to bind it. The AutoGenerate = "false" attribute ensures that the grid does not create all columns for you. If you want to be more complex, you can also use a template for this topic.
The previously mentioned Repeater and DataList controls also support templates, allowing you to customize the widget's appearance. The Repeater does not actually have an appearance. You must provide the UI, which means you must use a template. On the contrary, the DataList control is a list bound to data with a default appearance and rich behavior. The method for adding a template to these two controls is the same:
<Asp: DataList is = "DataList1" runat = "server">
<Template name = "HeaderTemplate">
Here "s your list of titles <br>
</Template>
<Template name = "ItemTemplate">
<% # DataBinder. Eval (Container. DataItem, "Title") %> <br>
</Template>
</Asp: DataList>
With this template, you can specify which HTML controls are used to form each part of the data bundle control. The names of five templates can be used with the DataList control: HeaderTemplate is used for the top part of the control, ItemTemplate is used for each project, and Alternating-Item-Template is used for other projects, separatorTemplate is used for the area between projects, and FooterTemplate is used for the bottom of the control.
The advantage of this system is that there are a large number of controls available for displaying the interface. Using the product list, you can use the code in Table 7 to generate the output shown in the following code:
Table 7 DaveSGrid2.aspx
<Asp: DataList id = "MyDataList" RepeatColumns = "2" runat = "server">
<Template name = "itemtemplate">
<Table cellpadding = 10 style = "font: 10pt verdana">
<Tr>
<Td width = 1 bgcolor = "BD8672"/>
<Td valign = "top">
Src = "<% # DataBinder. Eval (Container. DataItem," ProductImageURL ") %>">
</Td>
<Td valign = "top">
<B> Name: </B>
<% # DataBinder. Eval (Container. DataItem, "ProductName") %> <br>
<B> Description: </B>
<% # DataBinder. Eval (Container. DataItem, "ProductDescription") %> <br>
<B> Price: </B>
<% # DataBinder. Eval (Container. DataItem, "ProductPrice", "$ {0}") %>
</Td>
</Tr>
</Table>
</Template>
</Asp: DataList>
This code is quite simple. In addition to the DataList code shown above, more things are no longer needed. One thing worth noting in the Code is that you can specify the number of columns that appear, and the list will automatically process the column packaging. In this way, you only need to format the code, and the Web page is greatly improved, instead of using the traditional raster on the previous Web page.