Java Attack c#--application development ASP.

Source: Internet
Author: User

This chapter is a brief statement

In the previous chapter, I talked about the use of LINQ and EF. And we'll explain it in hibernate. So the author of this chapter describes the C # ASP. That is in the B/S mode development. Now most of the business is facing the B/S model. So the knowledge that is necessary for the understanding of ASP. When I was working on Java development, I seldom saw companies that were developed for AWT and swing. More are servlet and JSP development. This is also a matter of no means. Because using AWT and swing to develop software is not to say no. Just afraid of thankless. I'm not saying Java is bad. There are some areas where Java does not fit. Then the ASP can be understood as Java Web development. Both the HTML and the css,js look alike. Don't be careful about this. All right. Go inside the chapter.

Web Forms

We all know that the Java EE in the development process must be inseparable from Tomcat,jboss and other servers. The author compares good long with Tomcat, mainly because of his light weight. So what about ASP? What is he, a server? If it's just development. Readers are not to be careful that there is a self-contained IIS inside Visual Studio. Nothing wrong. The ASP. NET server is called IIS. Now let's create a new ASP.

About the new project now everyone knows. One thing to know here is that you should not forget to choose Visual Studio 2012 under the Web. Of course, the choice of the web is also possible. Just the options are not that much. I have only two here. Well, I want to create a new empty ASP to see.

So simple. An empty ASP is built. Now let's take a look at the new WebForm. Select Project Right-click on the Add Web form.

Click Web Form. Fill in the Item name: Index

The final result

Looking at the last picture above, we know that the Web form is actually an ASPX file. about what the aspx file is. JSP that can be understood as Java. is the place where the pages are assembled. All right. Let's write a simple display column. Double-click index.aspx. We can see the content. About the content will be discussed below. Add a code text to the contents of the display first. As follows

Index.aspx content:

<%@ page language= "C #" autoeventwireup= "true" codebehind= "Index.aspx.cs" inherits= "Webexample.index"%><! DOCTYPE html>

See it. You don't have to install any servers at all. It's easy to look at. Now the inside of the ecilpse seems to be equipped with Tomcat. However, it's best to pack your own local tomcat. Personally feel Ecilpse Tomcat is not good for use.

Once you've finished the example above, let's look at the contents of the Web form. There is a piece of code at the top of the Web form. As follows

<%@ page language= "C #" autoeventwireup= "true" codebehind= "Index.aspx.cs" inherits= "Webexample.index"%>

1.Language: Indicates what language is currently in use.

2.AutoEventWireup: Indicates whether the event is auto-bonding page. True indicates automatic. otherwise, false.

3.CodeBehind: Represents the back-end code facing the current page.

4.Inherits: Represents the code-behind class that the current Web form inherits from.

A few keywords above on the definition of a network piece. If you still do not understand the author writes. You can check it out for yourself. The author thinks that the keyword Codebehind is the best proof of the difference between JSP. Why is it? There's no such thing as a back-end code for JSP. This is one of the biggest differences with ASP. If we keep looking down. The following section of code appears.

The key point is not the head. The runat= "server" keyword behind the head. runat= "Server" is meant to indicate that this HTML tag is a server control. So what is a server control? The author understands that it is simpler to generate the corresponding HTML through the server analysis. Like the tag tag for Java. He has an intuitive difference than ordinary HTML tags are. Back-end code can not access the to. With the addition of runat= "Server", the backend code will be able to visit the corresponding property of the control. Like the form label above. Of course remember to increase the value of the property ID.

The above picture may not be able to read. It's okay. Why not let the author do a submission display example to illustrate what the backend code is?

First, the programming interface at the bottom. Select "Design". Pull a TextBox and a button, a label from the Toolbox. Is it a little like WinForm development? This is of course. Only the usage of the control and the environment look different. It is also important to note that you must pull the corresponding form tag inside. Otherwise there will be a problem.

Select "Design":

Pull control:

Second, double-click the button. Write the back-end code for the button.

Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.web.ui.webcontrols;namespace webexample{public    partial class Index:System.Web.UI.Page    {        protected void Page_Load (object sender, EventArgs e)        {        }        protected void Button1_Click (object sender, EventArgs e)        {this            . Label1.Text = "Hello" + this. TextBox1.Text;}}}    

The above code also uses the author to say more? See the red code to know. The input value of the textbox on the output page. Add "Hello" at the same time.

Three. Performs output printing.

It's so easy. This is the first time I feel that development is so easy. Back-End code I'm sure you understand. It's a little bit like the backend code developed for WinForm. In fact, there are two encoding modes for ASP. Above is the hidden code pattern. Another is called inline encoding mode.

For inline words the author went to try it. But found that many things in the book is not found. There would have been a place for us to choose when creating a new Web Form. I can't find it in my vs. But it's okay. The author also lists the code.

<%@ page language= "C #" autoeventwireup= "true" codebehind= "Index.aspx.cs" inherits= "Webexample.index"%><! DOCTYPE html>

In the original code, the author added two parts. As follows

  <script type= "Text/javascript" runat= "Server" >        protected void button2_click (object sender, EventArgs e)        {this            . Label1.Text = "Button2";        }    </script>
<asp:button id= "Button2" runat= "Server" onclick= "button2_click" text= "button"/>

We can see the Button2 event code. Not the backend but on the page. This time runat= "server" is even more important. You can understand that the script above is the back-end code. This is the server code.

Readers do not feel the shadow of the JSP can not see. This is normal. The biggest point of JSP is the code snippet. That is <%%>. In fact, learning the fastest and most suitable is not the person who learned Java. But the people who have studied ASP. Because ASP. NET contains many similar ASP syntax. All right. I create a new Web form and set it as the startup page to illustrate it.

Content of Default.aspx:

<%@ page language= "C #" autoeventwireup= "true" codebehind= "Default.aspx.cs" inherits= "Webexample.default"%> <! DOCTYPE html>

Since ASP. NET is an HTTP-based event-driven editor, it is necessary for a developer to understand the life cycle of the ASP. That is to understand the order in which he triggered the event. This way we can control the page and the business in the corresponding event.

PreInit event: This is the entrance to the life cycle. This time the page's controls have been instantiated. However, you can set the parent page or theme. (the author does not intend to speak about the master page and the topic.) Please see for yourself)

Init event: The parent page and theme settings are now complete. The ProcessRequest method of the generic handler is executed. (described below in the general handler) and recursively triggers the OnInit event for all child controls.

InitComplete Event: This event does something that is related to the view state. View state is the control information that was last stored. For example, after the text box input submission, the corresponding value will also be in the Style box. However, HTTP is stateless. Then why is this so? This is because ASP. NET stores the last view state. As follows.

Preload event: The official said that this time did not do anything. Just used to illustrate the next stage of the move.

Load event: This matter is very important. At this point, all the control's view state and data are ready to be completed. So access controls are the safest. Double-click on the page to enter.

LoadComplete event: Used for the page to indicate that the load is over.

PreRender event: Renders the output corresponding to the HTML before it occurs. Developers can do some articles in this matter. Of course it is worth noting that the PreRender event of the page is first triggered and then the child control.

Prerendercomplete event: The inverse is used to indicate that the PreRender event has ended.

Unload event: After rendering is finished, the corresponding object is processed. So the child controls are processed first, and then the page is processed.

The above is a simple explanation of the sequence of events about ASP. In fact, the reader can read the author after the book to find some specific to the blog for the study of ASP.

In the process of interface design, ASP. NET uses two classes of controls. are HTML controls (native HTML) and Web controls (the server controls above), respectively. This time I believe that everyone has a problem-there is no custom control like WinForm. Yes, of course. As for the approach is mostly similar. Select the corresponding project right click Add to create a new item. Then select the following. There are few writers for the content of Web Forms user controls. The main thing is to get to know about ASP. Java programmers know that there is something there.

General processing procedures

We learned about the content of Web Forms above. Know that the backend code is a bit like the servlet. The actual ASP. NET also has an application similar to Ava's servlet-a generic handler. What is a generic handler. See what the definition doesn't. Let's get to know him with a new general procedure. Select Project Right click Add General handler.

General processing Program click:

After the new success:

From the picture we can see that his end is not aspx. But the ashx. Double-click Handler.ashx and we can see the corresponding code. As follows

Using system;using system.collections.generic;using system.linq;using system.web;namespace WebExample{//    < summary>///    Handler Summary description///    </summary> public    class Handler:ihttphandler    {        public void ProcessRequest (HttpContext context)        {            context. Response.ContentType = "Text/plain";            Context. Response.Write ("Hello World");        }        public bool IsReusable        {            get            {                return false;    }}}

Execution Result:

Believe to see here. I do not have to say that people will understand. Except for the inability to visualize the design interface. The general business is implemented. It's similar to the servlet function. The ProcessRequest method above the code is used to process the request. Very similar to Java's Dopost and Doget.

Summary of this chapter

This chapter focuses on the content of ASP. But the point of knowledge about ASP is too big. Not the author of this series of eyes. So the author is just a simple introduction to the several Java-related and a little similar. And it is necessary to have the knowledge of the palm.

Java Attack c#--application development ASP. NET

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.