ASP.NET Reuse Code Technology - Code Binding Technology

xiaoxiao2021-03-06  113

Author: Su Hong is super REVIEW code to bind ASP.NET provides an important new technology. This article will show you how to use code binding techniques to implement the Separation of the web page representing layer and business logic code, and recommends that you use code binding technology to realize the reuse of code. In another article, we will give additional implementation techniques for implementing code reuse. Code Binding Technology in ASP.NET When you build your own ASP.NET application, the main advantage of using code binding technology is: it allows you to be easy to see (those HTML code) And server-side controls) with your performance code (these codes may be VB, C # or any other .NET supported language) free separation open. This advantage is more obvious when you have a multiplayer development team to develop the same project. A person may be responsible for the design and writing of the web page, while another person may be the development of the specific program responsible for the page. Thus, by dividing the respective operating range, the two sides can be easily achieved without destroying the work of the other party. Of course, to do the normal operation of the page, the good low-class color dive dive, when we use the code binding technology, the visualty code is existing in the file being an ASPX. . This is a new .NET extension, used to describe an ASP file. It is difficult to do any script in the previous ASP to have only HTML code (of course if you are meaningless). However, the code binding technology allows the ASPX file only contains HTML code and server-side controls, and allows developers to completely use object-oriented ways to implement a solution. This mechanism to access this ASPX file in a separate code file can achieve the above ideas. These files are compiled into a separate binary when the browser first issues a request, as shown below: The code binding file is a separate file that can be written using any .NET support. For example, use Visual Basic .NET to write these files, its extension will be "VB". This code binding code contains all events related to layers, functional functions, methods, and more. Each ASPX file can only be implemented by a code binding file. Of course, through a certain job, code binding technology can be used in a variety of environments that need to be reused code. Every Aspx file has their own code binding files, and multiple features similar ASPX files can share a public code binding file. This article shows how to achieve the most basic code binding technology of your ASPX file, and discuss how code binding techniques are applied to code reusable. In the subsequent article, we will introduce. Other technologies provided by the .NET architecture can also easily implement code reuse, such as user controls, compiled collections, and more. Let's take a look at an example to see how to implement code binding technology. For simplicity, our example will start with a simple search page. When we step by step through the code binding technology, we will be able to see how the code binding technology implements a code reuse ASPX file. In order to make the ASP.NET program running normally, you must install the .NET architecture beta1. If you want to make the program proposed in this article, you must install SQL Server2000. Step by step to implement code binding technology First, we need to build one. ASPX file. In this ASPX file, we need to use the @PAGE instruction to explain that we are binding a code. In order to do this, we need to set two properties: SRC and inherits. The src property specifies a file containing the actual code. If this property is not available, the class specified in the property inherits is looking for the parameters passed from the compile time. The inherits property specifies the class that exists in the source file. This class needs to be derived from the Page class.

Our specific examples are as follows: <% @ page language = "VB" autoeventwireup = "false" src = "search.vb" inherits = "Search"%> Next, we need to add an appropriate control to establish our user interface. Since our example page is a simple search page, we only need to join a small part of the control. In addition to some tag tags, we need to add a search text box, a button submitted, and a DataGrid control to display content in the database.

The following is the code: <% @ page language = "vb" src = "search.vb" inherits = "search"%>

WorldWide Books search

search criteria

<

asp: TableItemStyle HorizontalAlign = "Left" VerticalAlign = "Top" BackColor = "White"> The following is the running interface: Below, We need to build our code binding page. (If we use Visual Studio.Net Beta1 to build our ASPX file, then the system will automatically generate related code bind files while the ASPX is generated. In this article, our example used VB languages, of course, any other supported languages ​​(such as: C #, C ) can also be used. In order to make the code binding page work normally, some projects need to include in the source file. As follows: Some related collection name controls require the public classes you want to inherit by the ASPX file, and this class needs to inherit the statement of System.Web.ui.page control variables For our example, we need to reference system.web.ui. WebControls This namespace access to the class of the web control. To operate information in the database, we need to reference the two namespaces of System.Data and System.Data.sql. Quote System.Collections namespace Allows us to use hash tables to capture the application's related settings. With Microsoft.VisualBasic this namespace, we can use some VB's function libraries. Next, we need to build a public class makes our ASPX files to inherit it. The name of the class should be the same as the name specified by the property inherints in our ASPX file. This class should inherit System.Web.ui.page. By inheriting the above class, we can access the ASPX page. Finally, we need to declare the controls used in the program, especially our button control, so we can capture the click Click event of the button. In order to do this, we need to build these variables used to express our buttons and text boxes.

These variables need to be established using the WithEvents keyword, as follows: Protected WithEvents cmdSearch As System.Web.UI.WebControls.Button Protected WithEvents txtSearch As System.Web.UI.WebControls.TextBox so that our code to bind pages look as follows: Option Strict Off Imports System.Data Imports System.Data.SQL Imports System.Web.UI.WebControls Imports System.Collections Imports Microsoft.VisualBasic Public Class Search Inherits System.Web.UI.Page Protected WithEvents cmdSearch as System.Web. UI.WebControls.Button Protected WithEvents txtSearch As System.Web.UI.WebControls.TextBox Protected Sub Search_Load (ByVal Sender As System.Object, ByVal e As System.EventArgs) If Not IsPostback Then 'evals true first time browser hits the page End If End Sub End Class should notice that if we use Visual Studio.net beta1 to work, it will automatically help us generate most of the code. This will basically implement a page of pages that use code binding technology. For our example, it is clear that we need to join the actual code to truly implement the search and results display. So, we have to build a public sub-process, call this public sub-process when the search button is pressed. In our example, the corresponding code is approximately as follows: Public Sub cmdsearch_click (Byval E AS Object, Byval E AS System.EventArgs _ handles cmdsearch.click keyword handles Indicates that we will perform us when the CMDSearch button is triggered This public subsystem. (Another thing you need to do is that you need to monitor the value of page.ispostback in the page_load event is true. When monitoring the page.ispostback property, we also perform a real search job at the same time). The next code is just simple in the database PUBS, and returns the search result to the DataGrid control. What is the reusable performance? The result of the work we did is to separate our user interface from the program code. But what own incentive role? Code Binding Technology is a superior technology for organizational code, but it may only help with code reuse in some environments, mainly this technology is very suitable for building two for the same or When using the similar point of interest, this time it has played a good code reuse. For example, suppose you need to build two login pages for two different types of users, and two pages have a great difference (but actually playing). This is built to build two ASPX files, and they share the same code binding file is very sensible.

转载请注明原文地址:https://www.9cbs.com/read-100107.html

New Post(0)