Introduction to ASP. NET and Web forms

Source: Internet
Author: User

In ASP. NET web pages, user interface programming is divided into two different parts: visual component view) and logic combining model and controller. This division separates the Visual View of the page from the code model and controller behind the page that interacts with the page.

A visual element is called a Web form page. This page consists of files that contain static HTML server controls, ASP. NET Server controls, or both controls. The form page in this example consists of the following code:

 
 
  1. <%@ Page Language="C#" AutoEventWireup="true" Codebehind="Default.aspx.cs" 
    Inherits="PageController._Default" %> 
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  3. <html xmlns="http://www.w3.org/1999/xhtml"> 
  4. <head runat="server"> 
  5. <title>Untitled Page</title> 
  6. </head> 
  7. <body> 
  8. <form id="form1" runat="server"> 
  9. Name:<asp:TextBox ID="name" runat="server" /> 
  10. <p /> 
  11. <asp:Button ID="MyButton" Text="Click Here" OnClick="SubmitBtn_Click" 
    runat="server" /> 
  12. <p /> 
  13. <span id="mySpan" runat="server"></span> 
  14. </form> 
  15. </body> 
  16. </html> 

The Web form page logic consists of the Code created to interact with the form. The programming logic is placed in a file separated from the user interface file. This file is called "code hiding:

 
 
  1. using System;  
  2. using System.Web;  
  3. using System.Web.UI;  
  4. namespace PageController  
  5. {  
  6. public partial class _Default : System.Web.UI.Page  
  7. {  
  8. protected void SubmitBtn_Click(object sender, EventArgs e)  
  9. {  
  10. mySpan.InnerHtml = "Hello, " + name.Text + ".";  
  11. }  
  12. }  


This class provides the default implementation that can be overwritten by the derived class.

 
 
  1. using System;  
  2. using System.Web;  
  3. using System.Web.UI;  
  4. using System.Web.UI.WebControls;  
  5. namespace PageController  
  6. {  
  7. public partial class BasePage : Page  
  8. {  
  9. protected Label eMail;  
  10. protected Label siteName;  
  11. override protected void OnInit(EventArgs e)  
  12. {  
  13. //   
  14. this.Load += new System.EventHandler(this.Page_Load);  
  15. base.OnInit(e);  
  16. }  
  17. protected void Page_Load(object sender, System.EventArgs e)  
  18. {  
  19. if (!IsPostBack)  
  20. {  
  21. string name = Context.User.Identity.Name;  
  22. eMail.Text = DatabaseGateway.RetrieveAddress(name);  
  23. siteName.Text = "my cool site";  
  24. PageLoadEvent(sender, e);  
  25. }  
  26. }  
  27. // this method can be overridden by sub class.  
  28. virtual protected void PageLoadEvent(object sender, System.EventArgs e)  {
  29. }
  30. }
  31. }


The preceding sections describe ASP. NET and Web forms.

  1. ASP. NET TypeConverter
  2. Analysis on TypeResolver of ASP. NET
  3. Define JavaScriptConverter in ASP. NET
  4. How to replace Sys. Services in ASP. NET
  5. Use Profile Service of ASP. NET AJAX

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.