Since Microsoft launched the ASP.net, Code behind has become a hot topic for discussion.
code behind, is called the separation of codes, it can make your program appear more organized, more readable!
in the era of ASP rampant, programmers and art cooperation always seems to be less harmonious, a little page changes will make us a fuss. Once, I also tried to implement code separation in ASP, such as in the server to deal with the transaction in advance, and then use JavaScript to receive data, and then call. Although the function is realized, but the cost is even bigger, a little parody feeling.
now has the asp.net, the problem solved!
through a simple example, understand the code behind.
first to build a test.aspx page:
<%@ Page language= "C #" inherits= "Jeky" src= "Test.aspx.cs"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<TITLE> Code behind instance </TITLE>
<meta name= "generator" content= "EditPlus" >
<meta name= "Author" content= "jeky.cn" >
<meta name= "Keywords" content= "Jeky,asp.net,code behind" >
<meta name= "Description" content= "Code behind,2004-10-8" >
</HEAD>
<BODY>
<form runat= "Server" >
<br> Name:
<asp:textbox id= "TB" runat= "Server"/>
<asp:button id= "BT" text= "Submit" onclick= "Bt_click" runat= "Server"/>
</Form>
</BODY>
</HTML>
When the
page is open, the display looks like the following image:
can see that the top of the page code has this sentence:
<%@ Page language= "C #" inherits= "Jeky" src= "Test.aspx.cs"%>
where language= "C #" indicates which language compiler is used when the page is run, the Inherits property represents an ASPX page that derives from the Jeky class, and the SRC attribute indicates the file path to which the Jeky class belongs.
to implement the code behind function, it needs to be created with two files. One is the design page (such as *.aspx), and one is the code page (such as *.cs/*.vb). Look at the code page (Test.aspx.cs):
The
program has a using keyword in the first three lines, and is similar to the with in VB, where it is used to indicate the namespace in which the object is located. For example, the namespace to which the page class belongs is System.Web.UI, and the TextBox control is included in the namespace System.Web.UI.WebControls. You can reduce the input of your code and make the program look straightforward by using using to indicate the namespace you belong to.
immediately thereafter, declare a class called "Jeky", which inherits all the properties and methods of the page class.
protected is the object that is used in declaring programs.
void Bt_click is an "event handler" that requires a "public" keyword to be invoked in the design page.
The program will end up with a dialog box with content that is entered in the text box.
is just a small example, you may not realize the advantage of the code behind, as in the ASP in the same as the dim declaration variables. However, I suggest that you should develop a good habit in peacetime, a person is so, write Cheng also so.