Before applying ASP. NET code separation to website construction, we usually Design website pages in traditional website construction, and use development tools to design functions within the website framework. Such website construction has many drawbacks. The most prominent disadvantage is that it is not conducive to the joint development of groups, and the dependency between various links is too strong.
After ASP. NET code separation is used, we can use background encoding in ASP. NET to distinguish HTML User Interface Design colors and aesthetics from page code. In this way, we can solve the problem of parallel development in our group.
The topic idea is: The Artist designs the website pages, and the programmer develops the functions of the website in modules. After the page and function module are developed, you only need to slightly modify the HTML code on the art interface to complete the corresponding functions.
The following is an example of ASP. NET code separation.
The functions completed by programmers include the following modules:
1. Click Column1 on the left, and the three content in the middle shows the Group's three news items.
2. Click at Column2. The three items in the middle show the three persons.
3. Click Column3 to display a photo in the pop-up window.
To test these functions, we can assign them to two programmers.
Programmer A: Completion Module 1 and 2
1. You can first create a WebApplication and put two ImageButton: IBtnNews, IBtnMember and six labels: Lb1Title, Lb1Detail, Lb2Title, Lb2Detail, Lb3Title, and Lb3Detail on the interface. Generate a Web application and click IBNews. The six labels display the group news, click IBMember, and the six labels display the three members of the group. In this way, we have created conditions for creating source files.
2. Create a C # class file CodeBehind. cs.
3. Because we are creating a Web program, we need to add the System. Web. dll application in add reference.
4. Remove the constructor because no class is required for background encoding.
5. Let the class inherit the function from the Page object, that is
Public class CodeBehind: System. Web. UI. Page
6. copy the code in the generated WebApplication, which consists of two parts: the declaration part and the method part. Here, change the accessible-level protected in the application to public, this is because only external code can access our methods and variables. Note that all the controls on the page must have a corresponding local variable with the back-end encoding file users.
7. Generate a CodeBehind. cs.
So far, programmer A's work has been completed. Programmer B can generate his CodeBehind. cs file in the same way. A and B programmers synthesize code to complete a complete CodeBehinde. cs;
The Code is as follows:
- UsingSystem;
- UsingSystem. Web. UI;
- UsingSystem. Web. UI. WebControls;
- NamespaceCodetest
- {
- Public ClassNews: System. Web. UI. Page
- {
- PublicSystem. Web. UI. WebControls. Label Lb1Title;
- PublicSystem. Web. UI. WebControls. Label Lb1Detail;
- PublicSystem. Web. UI. WebControls. Label Lb2Title;
- PublicSystem. Web. UI. WebControls. Label Lb2Detail;
- PublicSystem. Web. UI. WebControls. Label Lb3Title;
- PublicSystem. Web. UI. WebControls. Label Lb3Detail;
- PublicSystem. Web. UI. WebControls. ImageButton IBtnNews;
- PublicSystem. Web. UI. WebControls. ImageButton IIBtnMember;
- PublicSystem. Web. UI. WebControls. ImageButton IBtnContact;
- Public VoidIBtnNews_Click (object sender, System. Web. UI. ImageClickEventArgs e)
- {
- ..............................
- }
- Public VoidIIBtnMember_Click (object sender, System. Web. UI. ImageClickEventArgs e)
- {
- ..............................
- }
- Public VoidIBtnContact_Click (object sender, System. Web. UI. ImageClickEventArgs e)
- {
- String strScript ="<Script language = javascript> \ n";
- StrScript ++ ="Window. alert ("+"\" Tel: 66763467 \""+");";
- StrScript ++ ="</Script>";
- Response. Write (strScript );
- }
- }
- }
The following describes how to combine the created background code with the developed webpage.
1. The integration engineer generates a new WebApplication, saves the CodeBehind. cs file in the bin directory, and adds it to the reference.
2. the integration engineer adds the pictures on the webpage of the artist to the corresponding reference, copies the HTML code, and puts them into the page of the new WebApplication. In this way, we can see that the beautiful page of the artist is displayed in our. in the aspx file.
3. Change the top yellow code on the page, where Codebehind = "CodeBehind. cs": point the code supported by the page background to the prepared cs file. Inherits = "codetest. CodeBehind": Let the page inherit from the functions in the CodeBehind class. codetest is the defined noun space.
4. in the HTML code, enter <body> in <form id = "Form1" method = "post" runat = "server"> Add </form> On </body>.
5. Drag the corresponding part into the Web control. Note that the ID here must correspond to the definition in the cs file.
6. In the HTML code, find the Web Control and add the corresponding method name.
This completes the integration. Run the command to see how it works.
As you can see, it is a little troublesome to modify the HTML code above. We also have a simpler method, as long as several lines of code are added to the background file, we don't have to find the control location in HTML and add event references.
Add
- ProtectedOverrideVoidOnInit (EventArgs e)
- // This method triggers the Init event when the server control is initialized.
- {
- Control initialization method)
- Base. OnInit (e );
- }
- Private VoidControl initialization method );
- {
- This. Control name. Click + =NewEventHandler (control Event Response Method );
- }
The above two methods are added. We can see that as long as we add the initialization control method to the background code, we can add the corresponding events, instead of adding event references to HTML code. Similarly, we can implement common Page_Load events.
Just join:
- PrivateVoidPage_Load (ObjectSender, System. EventArgs e)
- {
- Code;
- }
- Private VoidControl initialization method );
- {
- This. Control name. Click + =NewSystem. EventHandler (control Event Response Method );
- This. Load + =NewSystem. EventHandler (This. Page_Load );
- }
EventHandler: indicates the method to process events that do not contain event data.
The control event response method only needs to comply with: method name object sender, System. EventArgs e.
ASP. NET code separation currently has the following problems:
1. How to ensure that the effects made by the artist are not affected by the use of Web controls.
2. When multiple people make the same webpage, they can only integrate the background code by merging the cs file. It is not conducive to code maintenance.
The role of ASP. NET code separation in website construction will be introduced here first, hoping to help you.
- Analysis on the use of ASP. NET data verification controls
- Analysis on five common controls for ASP. NET data verification
- Some discussions about ASP. NET code separation
- Experience in ASP. NET code separation
- Detailed research on ASP. NET data verification technology