Savor asp.net

Source: Internet
Author: User
Tags eval extend new features new set require requires web services domain
asp.net ASP is now rebuilt from head to toe, and the result is asp.net. It's not just ASP 4.0, it's a completely new framework for Web development, which includes many new features. Asp. NET provides more easily written, more structured code that is easy to reuse and share; ASP.net uses the compiled language to improve performance and scalability; ASP.net uses Web forms to make development more intuitive and uses object-oriented technology to facilitate reuse of components. In addition, ASP. NET also includes page events, Web controls, buffering techniques, and server controls, as well as improvements to data bundling. For ASP. NET and the Microsoft.NET framework allows you to use the customer business functions through the web, providing more new development opportunities for programmers.

 

Asp. NET's advantages

 

Asp. NET makes the code cleaner. Existing ASP applications, regardless of how you write them, the structure is always full of long and short code. asp.net code is not only easier to write, but also cleaner and easier to read than ASP code. At the same time, ASP. NET code improves its reuse and sharing.

asp.net improves configuration, scalability, security, and reliability. For simple ASP applications, configuration is not a problem, but when you migrate to an n-tier structure that utilizes components, you may experience problems. When you configure and maintain these applications, DLL trap issues (component registration, version, locked DLLs, and so on) appear. Asp. NET, the component registration and DLL locking are eliminated, and the XML configuration file is fully used, which solves this problem. This allows you to configure a Web application only if you are working on a copy file.

Asp. NET provides better support for various browsers. For ASP developers, browser compatibility issues seem to be an eternal problem. You either write code for a lower-level browser, such as using HTML 3.2, or restricting the browsing scope of the page. Wireless Application protocol The introduction of WAP devices will also deepen this problem. This part of the Web Form in this article describes how asp.net solves browser compatibility issues.

ASP.net created a new type of Web application. Current Web applications are usually the same pattern: a linear application, and then the logic is implanted into one of them. Asp. NET allows developers to break this single pattern and create more dynamic and scalable applications that better meet the business needs of your company and provide a richer development environment.

Here you may think that while these goals are noble, they make it difficult to write applications. Then, on the contrary, it is much simpler to develop with asp.net!

Let's take a moment to savor the powerful features of ASP.net, including the following:

Support for the compiled language

Web Forms

Page events following a Page object

Web control

Web Services

Buffering Technology

Debugging and tracing

Code and Content Isolation

Generic Libraries for namespaces

Configuration

 

Using a compiled language

 

The existing ASP versions are based on scripting languages, such as VBScript and JScript. There is nothing wrong with the scripting language itself, but they have two major drawbacks: they are interpretive and not strictly classified. Asp. NET does not completely discard the concept of scripting languages, it introduces support for a fully-compiled language, enabling you to write your own server-side code in Visual Basic, for example:

<script language= "VB" runat= "Server" >

One of the great advantages of Visual Basic is its support for strict classification variables, so the following code is set up in asp.net:

Dim FirstName as String

In addition to Visual Basic and C + +, you can use the latest Microsoft Language C # to write server-side code. C # Removes the impractical parts of C + +, which is easier to understand. With ASP.net, the compilation action occurs the first time the page is loaded. Even if the code is a scripting language, it is compiled before execution, so the performance of the JScript code page is improved. In fact, this is a fundamental new feature of the. NET Framework. The previous version of the language compiler treats data types and objects differently, meaning that the only thing that can be developed across languages is to create COM objects. Microsoft. NET Framework the CLR allows for close interaction with any code that has been compiled with Universal Runtime support, which is what the new Visual Basic and C # compilers will do: Create code that can be managed at run time.

The great advantage of doing this is making real cross-language development possible. With the universal runtime environment CLR, you can create objects in C # and extend them in Visual basic by inheriting functionality. Now the Visual Basic. NET supports inheritance so that you can write components in C # and then set them to subsets in Visual Basic. Although Visual Basic, C #, and scripting languages are supported in the starting frame structure, other languages later, such as Smalltalk, Eiffel, Pascal, and so on, will also be supported. Microsoft. NET Framework is very easy to extend, so using a new language only requires compiler support for output that is compatible with the Run-time environment.


Web Forms



asp.net Web Forms are web pages, the same work you do with ASP writing code now. But more than that, ASP.net Web forms is designed on an object-oriented programming model so that the code can be recycled and the application code is separated from the content of the page. In Visual Basic, you drag a control onto a form, and then execute the event program under it. This is not possible in traditional ASP, because there is no link between the user interface controls and their server-side code. But in asp.net, such a link exists. So, you can write the code shown in table 1 below instead of dragging the value out of the variable table manually:

Table 1 simple asp.net Web form routines


<script language= "VB" runat= "Server" >

Sub SubmitButton_Click (Source as Object, E as EventArgs)

Response.Write (òyou enteredò& Name.text)

End Sub

</script>

<body>

<form method= "POST" runat= "Server" >

Name: <asp:textbox id= "name" runat= "Server"/><br>

<asp:button text= "Enter"

onclick= "SubmitButton_Click" runat= "Server"/>

</form>

</body>


Two points in table 1 are noteworthy. The first is to runat= the use of the "Server" property and the Asp:textbox control on the form, which tells ASP.net the server and the customer can use the controls. The control so used is called the server control. asp: Used as a prefix for the name of a control, which itself identifies where the control came from. I will discuss this later. The second detail to note is the onclick event. When developing DHTML code, you often use the OnClick event to activate an event in the browser. Because the runat= "Server" property is set for the control, the event is only activated on the server. To extend this example, you can remove the Response.Write and replace it with a server-based control, such as the following table 2 code:

Table 2 Using Server-based controls


<script language= "VB" runat= "Server" >

Sub SubmitButton_Click (Source as Object, E as EventArgs)

Youentered.text =òyou enteredò& txtName.Text

End Sub

</script>

<body>

<form method= "POST" runat= "Server" >

Name: <asp:textbox id= "txtname" runat= "Server"/><br>

<asp:button text= "Enter"

onclick= "SubmitButton_Click" runat= "Server"/>

<br>

<asp:label id= "youentered" runat= "Server" ></span>

</form>

</body>


This code works pretty well, much like a traditional customer or Visual Basic form, and the code is much more intuitive. You can use server controls to connect event procedures and server-based code. These server-based controls send pure HTML content to the browser, which no longer contains client script. In fact, one of the important design goals is to stick with the inherent HTML 3.2 element code to provide the maximum possible browser compatibility. For example, the Code in table 2 generates the following HTML:


<body>

<form name= "HtmlForm2" method= "post" action= "Test.aspx" id= "HtmlForm2" >

<input type= "hidden" name= "__viewstate" value= "a0z664351470__x" >

Name: <input name= "txtname" type= "text" id= "Txtname" ><br>

<input type= "Submit" Name= "Button5" value= "Enter" >

<br>

<span id= "lblyouentered" ></span>

</FORM>

</body>


The generated code is compliant with the HTML 3.2 standard. It performs a standard post and sends user input back to the same file. There is no maintenance of the server state and no client script to maintain the state. The suppressed domain performs maintenance on the state of the control, which means that the control can automatically recover the state, without any programmatic intervention, between the "Commit-return" page. Although the default output of the ASP.net Web control is content that conforms to the HTML 3.2 standard, it can also be exported to a DHTML format for more advanced browsers, such as Microsoft Internet Explorer 5.0. This allows you to write pages using only a set of server controls, allowing the control to send the type of output based on the browser's decision, allowing you to use the control and client script that sends DHTML to the latest version of Internet Explorer, sending pure HTML 3.2 content to other browsers.



Page Events



Before this I mentioned ASP. NET has been rewritten from head to toe, but I'm not pointing out that it was rebuilt in an object-oriented way. At the top of the object tree is the Page object, which is the paging object, ASP. NET each control, application, and page is inherited from this object, which means that each page is an example of a Page object. The load (load) event for the page is a very important event, as shown in the following table 3 code:

Table 3 Using Page events


<script language= "VB" runat= "Server" >

Sub Page_Load (Source as Object, E as EventArgs)

' Code to run when page loads

End Sub

Sub SubmitButton_Click (Source as Object, E as EventArgs)

' Code to run ' when the button is clicked

End Sub

Sub Page_Unload (Source as Object, E as EventArgs)

' Code to run when page unloads

End Sub

</script>

<form runat= "Server" >

<asp:button text= "Enter" onclick= "SubmitButton_Click" runat= "Server"/>

<asp:label id= "youentered"/>

</form>


Here you see the same load/unload (load/unload) process that you've seen in Visual Basic. When the page is loaded, the Load event is activated, and all server-based controls are available. Other events are generated during interaction with the user. Finally, the Unload event is activated when the page is unloaded.



Web control



You may be concerned that controls like <asp:TextBox> represent a new set of controls that require your proficiency. However, they are not difficult to learn because they have counterparts in HTML. For example, for a very simple text box, in HTML, you do this:

<input type= "text" value= "Your Name" ></input>

and the corresponding Web controls are like this:

<asp:textbox text= "Your Name" runat= "Server"/>

In both cases, you immediately notice that the Web control is identified by the code "ASP:" namespace and, as in XML, uses a slash to end the element. You do not have to use XML format, you can also use HTML format, with a semicolon to end the tag:</asp:textbox>. But you will find that the XML format is used in a number of code examples, and the code is more concise words. The namespace must be used, which is responsible for identifying where the text box TextBox control comes from. All standard Web controls are part of the ASP namespace. This becomes important when writing your own controls.

The TextBox control does not seem to have much advantage over the standard input frame, but you should consider the three input controls on the face:

<input type= "Text" ...>

<input type= "Password" ...>

<textarea rows= "5" ...>

They are all 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 functionality of three controls in HTML and is easier to remember and encode.

Asp. NET carries 5 types of Web controls:

• Intrinsic controls corresponding to HTML

• List control that provides data flow on the page

• Rich controls that provide richer UI (user interface) content and functionality

• Complete validation controls for various forms

• Package WML mobile controls for WAP devices

Intrinsic server controls are the same as HTML controls, but are more logical and provide a more consistent use. These controls include LinkButton (link buttons), ImageButton (image buttons), HyperLink (hyperlinks), TextBox (text box), CheckBox (check box), RadioButton (checkbox), DropDownList (Drop-down list), ListBox (list box), image (image), label (label), Panel (panel), table (table), TableRow (table row), TableCell (table cell).

list controls include repeater (forwarders), DataList (data lists), and DataGrid (data grid). List controls also include RadioButtonList (List of radio boxes) and CheckBoxList (check box list), making it easy to create lists of radio boxes and check boxes.

Rich controls include calendar (Calendar) and AdRotator (ad rotation). The Calendar control outputs pure HTML for a low-level browser and outputs DHTML for advanced browsers (such as Internet Explorer 5.0). AdRotator output image, it has the built-in rotation code.

The validation controls include RequiredFieldValidator (request domain acknowledgement), Compare Validator (comparison confirmation), RangeValidator (scope acknowledgement), RegularExpressionValidator (Specification expression confirmation), CustomValidator (Customer confirmation) and ValidationSummary (confirmation summary). These controls provide a simple way for developers to establish validation in form processing.

Information about mobile controls has not yet been published, but it is certain that they help to construct a Web site that activates WAP.



Writing a new control



You are not necessarily limited to using the controls provided by these systems and writing controls yourself is fairly straightforward. For example, if you want a control to encapsulate two text boxes (possibly the input fields for first and last names), you can write code like this:

<asp:panel runat= "Server" >

<asp:textbox id= "txtFirstName" text= "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 this new extension) and treat it as a Web Form control. Next, you can add the following to your Web form:

<%@ Register tagname= "Namecontrol" tagprefix= "Foo" src= "NAME.ASPC"%>

<form>

<foo:namecontrol runat= "Server"/>

</form>

This allows you to easily create a control that can be used again. It's really nice;--) You can also create controls directly in Visual Basic or C #, allow them to be subclasses of other controls, and render any output they require. Because controls are identified by namespaces, there should be no conflict between controls. In fact, controls can even use the same name, as long as they are in a different namespace. You will feel that this makes the ASP. NET is very extensible and makes the programming environment more and more rich. There is actually 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 the SQL generated data, you only need to create the Ado+ object and execute instructions to get the data as a grid data source, such as the following table 4 code:

Table 4 davesgrid1.aspx

<%@ Import namespace= "System.Data.SQL"%>


<script language= "VB" runat= "Server" >

Sub Page_Load (Sender as Object, E as EventArgs)

Dim MyCommand as SQLCommand

mycommand = New SQLCommand (Òselect * from Products),

Òserver=localhost; Database=advworks; Uid=sa ")

DataGrid1.DataSource = Mycommand.execute

Datagrid1.databind

End Sub

</script>

<body>

<asp:datagrid id= "DATAGRID1" runat= "Server"/>

</body>


All you have to do is bundle the data into a data grid and then generate a neat HTML table:



Data bundles are not limited to data from databases, you can bundle them into hash tables, arrays, other server controls, and the appropriate layer of the page, almost anything. If the default column is not appropriate, you can also customize it to show the part 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 bundle the column. The autogenerate= "false" attribute is responsible for ensuring that the grid does not create all the columns for you. If you want to be more complex, you can also use a template for this column.

The Repeater and DataList controls mentioned earlier also support templates, allowing customization of the appearance of the control. Repeater actually doesn't look, you have to provide the UI, which means you have to use a template. Instead, the DataList control is a list of bundles to data, with a default look and rich behavior. The method for adding a template to the 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 template, you can specify which HTML controls are used to make up each part of the data bundle control. The names of 5 templates can be used with the DataList control: HeaderTemplate for the topmost part of the control, ItemTemplate for each item, alternating-item-template for other projects, SeparatorTemplate is used for areas between items, FooterTemplate for the bottom of the control.

The advantage of this system is that there are a lot of controls available for how to display the interface. Using the Product list further, you can use the Code in table 7 to generate the output shown in the following illustration:



Table 7 davesgrid2.aspx

<asp:datalist id= "Mydatalist" repeatcolumns= "2" runat= "Server" >

<template name= "ItemTemplate" >

<table cellpadding=10 style= "font:10pt Verdana" >

<tr>

&LT;TD width=1 bgcolor= "BD8672"/>

&LT;TD valign= "Top" >


Src= "<%# DataBinder.Eval (Container.DataItem," Productimageurl ")%>" >

</td>

&LT;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 fairly simple, and there's no need for anything more than the DataList code shown earlier. One point in the code that is noteworthy is that you can specify the number of columns that appear, and the list automatically handles the packaging of the columns. This requires only a bit of formatting code, and the Web page is greatly improved, instead of using traditional grids on previous web pages.




Developing Web Services (Web service)



Software that is published on the Internet as a service is at the heart of Web services. Asp. NET provides a basic infrastructure environment for WEB services, enabling developers to create services for this service model.

Now let's track down a simple example. For example, if you are buying books from an online bookseller, they have a tracking system that allows you to see the status of your order. Book Commercial a land transportation company to transport the goods you ordered. The transportation company also has a tracking system. In this way, in order to get the exact status of your order, you have to view two sites. It would be convenient if booksellers could display their order status and the status information of the shipping company together.

Web Services allows you to expose customer business functions to the public on the web, such as parcel tracking details. Write an object that exposes its method as a URI, and the URI returns an XML data. Now booksellers can call the shipping company's tracking service and incorporate the results of the tracking into its own order tracking application. Here's how the shipping company uses C # to create a service code:

<%@ WebService language= "C #"%>

Using System.Web.Services;

public class Shipping {

[WebMethod]

public string Orderstatus (string ordernumber) {

Code here to retrieve the order details from data store

return Status;

}

}

The above code is stored in the Tracking.asmx file in the application directory of the shipping company web site. This allows booksellers to invoke this WEB service in a variety of ways. For example, using Http-get, parameters are passed along with the query string:

Http://orders.ups.com/orders/Tracking.asmx/OrderStatus?OrderNumber=BRU123

Booksellers can also use Http-post, which is passed as a form value in the POST body. Alternatively, Http-soap can be used, and the parameters of the method are wrapped in an industry-standard XML format.

Now users only need to check their order details at the booksellers, and the Bookseller's WEB application collects tracking information from the shipping company and returns with the status of the book. Booksellers can also expose their order status details as a Web service and let others use it.

The great thing about Web services is that it allows you to expose a service without exposing the data or all the business rules. Both code and data are safe when the business services are automatically provided. Over the next few years, with the availability of business-to-business solutions, the market for WEB services will grow rapidly.


Related Article

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.