Java attack C # -- Application Development Asp.net,

Source: Internet
Author: User

Java attack C # -- Application Development Asp.net,

Summary of this Chapter

In the previous chapter, I talked about the usage of Linq and EF. And use hibernate to explain. In this chapter, I will talk about Asp. Net in C. It is developed in B/S mode. At present, most of the business of an enterprise is oriented to the B/S model. Therefore, understanding Asp. Net becomes essential. When I am engaged in JAVA Development, I seldom see enterprises that are engaged in Awt and Swing development. More are Servlet and JSP development. There is no way to do this. Because it is not impossible to develop software using Awt and Swing. It's just a fear of thankfulness. I am not saying that JAVA is not good. Some aspects of JAVA are indeed not suitable. Asp. Net can be understood as JAVA Web development. Javascript is the same as HTML and CSS. Don't worry about this. Okay. Go to the internal section of this chapter.

Web Forms

We all know that J2EE is inseparable from servers such as Tomcat and JBoss during development. The author is better at using Tomcat because it is lightweight. What about Asp. NET? What is his server? If it is only development. Readers should not be careful that visual studio has a built-in IIS. No error. The Asp. NET Server is called IIS. Now let's create an Asp. NET project.

Now everyone knows about creating a project. Here, you need to know that you should not forget to select Visual Studio 2012 under the Web. Of course, you can also select Web. There are not so many options. I only have two. Now, I want to create an empty Asp. NET file.

Easy to use. An empty Asp. NET is created. Now let's look at a new WebForm. Right-click the project and choose add Web form.

Click Web form. Enter the item name: Index

Final Result

Looking at the final image above, we know that the Web form is actually An aspx file. What Is An aspx file. Java jsp. Is the place where the web page is assembled. Okay. Let's write a simple display column. Double-click Index. aspx. We can see the content. The content is described below. Add a code text to the content. As follows:

Index. aspx content:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="WebExample.Index" %><!DOCTYPE html>

Display result:

See it. No servers are required. This is very convenient. Now ecilpse also seems to have Tomcat installed. However, you 'd better install your local Tomcat. I personally think ecilpse Tomcat is not easy to use.

After completing the above example, let's take a look at the content of the Web form. There is such 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 the current Language.

2. AutoEventWireup: Indicates whether to automatically bond page events. True indicates automatic. Otherwise, the value is false.

3. CodeBehind: indicates the backend code of the current page pair.

4. Inherits: indicates the hidden code class inherited by the current Web form.

The preceding keywords are defined on the network. If you still don't understand what I wrote. You can view it on your own. The author believes that the keyword CodeBehind is the best proof that it is different from JSP. Why? JSP has nothing to say about backend code. This is also one of the biggest differences with Asp. NET. If we continue to look at it. The following code is displayed.

The key point is not the head. The runat = "server" keyword after the head. Runat = "server" is used to indicate that the HTML Tag is the server control. So what is a server control? The author simply analyzes the server to generate the corresponding HTML. For example, JAVA Tag. There is an intuitive difference between HTML tags and HTML tags. Can backend code be accessed. After runat = "server" is added, the backend code can access the corresponding properties of the control. The Form label above. Of course, remember to add the attribute ID value.

The image above may not be readable. It's okay. Let me give you an example of submitting and displaying the backend code ?.

I. At the bottom of the programming interface. Select Design ". Pull a Textbox, a button, and a Label from the toolbox. Is it a bit like WinForm development? Of course. The usage of the control is different from that of the environment. In addition, be sure to pull it to the corresponding form tag. Otherwise, problems may occur.

Select "design ":

Pull control:

2. Double-click the Button. Write the back-end code of 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;        }    }}

Do I have to say more about the above Code? You can see the red code. Output the input value of TextBox on the webpage. Add "Hello ".

3. Execute output printing.

It's easy. This is the first time I think development is so easy. The backend code is clear to everyone. The backend code corresponding to WinForm development is a bit like that. In fact, there are two encoding modes for Asp. NET. The above shows the hidden code mode. In addition, it is also called the inline encoding mode.

For inline, I tried it. But I found that I could not find many things in my book. When creating a Web form, we have a location for selection. I cannot find it in. But it's okay. I also listed the code.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="WebExample.Index" %><!DOCTYPE html>

I added two parts in the original code. 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 event code of button2. It is not on the backend but on the webpage. In this case, runat = "server" is even more important. You can understand that the above script is the back-end code. That is, the server code.

Do readers think they cannot see the shadows of JSP. This is normal. The most important part of JSP is the code segment. That is, <%>. In fact, the fastest and most suitable person to learn Asp. Net is not a JAVA student. But the person who has learned Asp. Because Asp. Net contains many similar Asp syntaxes. Okay. Create a Web form and set it to the startup page.

Content of Default. aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebExample.Default" %><!DOCTYPE html>

Is it very similar. Sorry. Common Asp. NET development does not use the above Code segment. The main role is the server control. So if you want to use Asp. NET. You may want to know about an Asp. NET control. It is the learning toolbox. I will not talk about him here.

I don't know what readers think about the code above. When I first saw it, I didn't have much idea. I think it is very good. But when I think about it further. I found out that it seems like JSP + Servlet. Later, I also read some materials. Asp. NET introduces thing-driven programming. So we can see the click button bound above. This is very different from JAVA. We all know that even JSP + Servlet. Generally, a Form tag in JSP corresponds to a Servlet. What does it mean? JSP is used for HTML interface design, while Servlet is used for processing business code. Therefore, in general, JSP has only one Form tag. What can I do if there are multiple tasks? More than one parameter is used to represent an action. For example, http: // xxxxx? Action = add. However, with Asp. NET introduced thing-driven programming, we don't need to operate like JAVA. You only need to bind the corresponding event. For example, the click event is bound to the above. When a button is clicked on a webpage, the corresponding event is triggered. This is similar to WinForm operations. As for what sending back is, I can only simply say that it means submitting the backend at one time. We often use Page. IsPostBack to determine whether the request is sent back. For the first time, I want to do some business. In short, the Web form interface design is equivalent to JSP, And the backend code is equivalent to Servlet.

Protected void Page_Load (object sender, EventArgs e) {if (Page. isPostBack) {this. label1.Text = "not the first time <br>";} else {this. label1.Text = "First time <br> ";}}

Since Asp. NET is HTTP-based event-driven editing, it is necessary for a developer to understand the lifecycle of Asp. NET. That is, understanding the trigger sequence of the event. In this way, we can control pages and services in corresponding events.

PreInit event: This is the entry to the lifecycle. At this time, the page control has been instantiated. However, you can set the parent page or topic. (I am not going to talk about the parent page and subject. Please check it by yourself)

Init event: the parent page and topic settings are complete. Generally, the ProcessRequest method of the processing program is executed. (As described below in general) and recursively trigger the OnInit events of all child controls.

InitComplete event: this event is related to the view status. View status is the control information stored last time. For example, after the text box is submitted, the corresponding value will also be in the style box. However, HTTP is stateless. So why. This is because Asp. NET stores the last view status. As follows.

PreLoad event: the official website said that nothing was done at this time. It is only used to indicate that you want to enter the next stage.

Load event: This is very important. At this time, the view status and data of all controls are ready. Therefore, the access control is the safest. Double-click the page to enter.

LoadComplete event: used for pages to indicate that the loading is complete.

PreRender event: events that occur before the corresponding HTML is displayed. Developers can make some articles in this case. Of course, it is worth noting that the page PreRender event is first triggered, and then the child control.

PreRenderComplete event: reverse is used to indicate that the PreRender event has ended.

UnLoad event: After the rendering ends, the corresponding object is processed. Therefore, the child control is first processed and then processed on the page.

The above is a brief description of the sequence of Asp. NET events. In fact, after reading this article, readers can find some blog posts dedicated to Asp. NET.

Asp. NET uses two class controls during interface design. These are HTML controls (native HTML) and Web controls (server controls described above ). At this time, I believe everyone will have a question-is there any custom control like WinForm. Of course. Most of the practices are similar. Right-click the corresponding project and choose add> new item. Then select the following. There is not much content for Web forms user controls. I mainly want to know about Asp. NET to make it easier for JAVA programmers to know that it exists.

General Handler

We have learned about Web forms. The backend code is similar to Servlet. Actually, there is an application in Asp. NET that is very similar to AVA's Servlet-a general processing program. What is a general handler. The definition is useless. Let's take a look at a general processing program. Right-click the project and choose Add General handler.

Click the General handler:

After the creation is successful:

We can see from the figure that its end is not aspx. But ashx. Double-click Handler. ashx to view the corresponding code. As follows:

Using System; using System. collections. generic; using System. linq; using System. web; namespace WebExample {/// <summary> // Handler abstract 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:

I believe you can see it here. I don't need to say that everyone will understand. In addition to visual design interfaces. Generally, all services are implemented. It is similar to Servlet functions. The ProcessRequest method above the code is used to process the request. Similar to JAVA doPost and doGet.

Summary of this Chapter

This chapter focuses on Asp. NET. However, the knowledge about Asp. NET is too large. It is not the author's series of projects. Therefore, the author just briefly introduces several Asp. NET-related items which are somewhat similar to those of JAVA. It is also necessary to grasp the knowledge of data.

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.