Asp. Code Separation in net

Source: Internet
Author: User
Tags command line
Asp.net| code to separate the ASP. NET contains a new approach: Separating business logic code from the expression code. This is often referred to as the code behind, which is very powerful and very easy to implement. The implementation step is to add user interface elements to the ASP.net page and assign them the property "Runat=server". Then, create a class file in the. NET language to manipulate these user interface elements. Finally, add a directive at the top of the ASP.net page to attach the user interface to the class file that operates it.

Use a simple example to show how it is done. Next, create a asp.net page called webpage.aspx, where you paste the following code:

<%@ Page language= "VB" inherits= "DotNet101.MySample.WebPage"%>
<title>code-behind demo</title>
<body>
<form id= "Messageform" runat= "Server" >
<asp:textbox id= "message" runat= "Server"/>
<asp:button id= "Submit" onclick= "Submit_onclick" text= "Send message" runat= "Server"/>
</form>
</body>

Note that the "Inherits" attribute, added to the "page" directive, is responsible for notifying the asp.net that the page should use the "DotNet101.MySample.WebPage" class for its business logic. We have added two ASP.net server controls to this page, and have specified the "Runat=server" attribute for them and the form control itself. These are the controls behind the code class that will be manipulated. When the user clicks on the "Submit" button, the "OnClick" attribute assigned to the button server control tells the ASP.net engine to activate the Submit_onclick event handler in the code class behind it.

Below, we need to create a code class file behind to manipulate the ASP.net page. Create a file named Codebehind.vb, and the Visual Basic.NET code for the class is as follows:

Imports System
Imports system.web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Namespace Dotnet101.mysample
Public Class webpage:inherits Page
Protected Messageform as HtmlForm
Protected message as TextBox
Public Sub Submit_onclick (sender as Object, E as EventArgs)
Dim Output as Label = New Label
Output. Text = "You just typed:" & message. Text
MESSAGEFORM.CONTROLS.ADD (Output)
End Sub
End Class
End Namespace

In this class file, we introduce a lot of namespaces to help behind the process of code. If we do not introduce these namespaces, we do not have the right to use them. NET object to work behind the code. Then we want to specify our own custom namespaces. So, if we want to, we can include more than one behind the code class in a file. Otherwise, each behind the code needs to be stored in its own file, and you need to specify the file in the "SRC" attribute of the asp.net page "page" directive.

Our webpage class is inherited from the normal page (page) object, so it has the right to use its intrinsic services. This allows you to interact with the ASP.net page and, if necessary, place events in page (page) layer events, such as Page_Init and Page_Load. In this example, we just want to respond to the click of the Submit button on the ASP.net page, so we execute the Submit_onclick event handler that was previously referenced in the ASP.net page. Inside the event handler, we declare and initialize a new label (label) server control. Then we grab the contents of the Message textbox (Information text box) server control and assign it to the new label (label) server control. Finally, we add a label (label) server control to the controls (control) collection of the form (form) server control. This way, when the page is reloaded, the information is displayed on the page.

Note that we declare protected variable "Messageform" and "message" to hold references to their corresponding controls on the ASP.net page, which enable us to see and manipulate controls from behind the code class. In fact, the underlying code class is higher on the genetic level than the ASP.net page itself, so the corresponding controls on the ASP.net page simply inherit the declaration in the code class behind the simple "execute".

The final step of the example is to compile the code class behind it, which can be done by using the following instructions at the command line:

Vbc/t:library/out:dotnet101.mysample.dll
/r:system.dll,system.web.dll Codebehind.vb

After this instruction is run, a file named "DotNet101.MySample.dll" appears in the directory, and then the file is placed in the bin directory of the ASP.net site.

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.