zoukankan      html  css  js  c++  java
  • 第一个asp.net实例——生日邀请以及回函

      22回校后,看了论文游了西湖,今天开始接触asp.net,从图书馆选了两本书:《精通ASP.NET 4.5 (第五版)》,《ASP.NET全能速查手册》。一本练手细看,一本翻查。

      跟着第一章敲,顺便学VS2012。实例包括文件

      

    • Default.aspx作为主页面,页面内容包括表单的文本域,下拉选项,以及重要的提交按钮。

      

      aspx文件本质上还是html文件,只在页头附加<%和%>标签的元素声明,head以及form元素中添加runat="server"。

    • aspx作为呈现内容,并未有与服务器沟通的实质性代码,此时利用<%和%>内的CodeBehind特性,启用aspx文件相应的隐藏文件,后缀名为aspx.cs的文件。该代码隐藏类的基础是System.Web.UI.Page,其中包含许多用于响应Web请求的方法和属性。在Default.aspx.cs代码隐藏文件中,利用在初次加载页面时就会被调用,并在用户提交窗体时再次被调用的Page_Load方法,对用户是否参加晚会进行判断,从而转向相应的结果页面。
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.UI;
     6 using System.Web.UI.WebControls;
     7 using System.Web.ModelBinding;
     8 
     9 namespace PartyInvites{
    10     public partial class Default : System.Web.UI.Page{
    11         protected void Page_Load(object sender, EventArgs e){
    12             if (IsPostBack){
    13                 GuestReponse rsvp = new GuestReponse();
    14                 if(TryUpdateModel(rsvp,new FormValueProvider(ModelBindingExecutionContext))){
    15                     ResponseRepository.GetRepository().AddResponse(rsvp);
    16                     if(rsvp.WillAttend.HasValue && rsvp.WillAttend.Value){
    17                         Response.Redirect("seeyouthere.html");
    18                     }else{
    19                         Response.Redirect("sorryyoucantcome.html");
    20                     }
    21                 }
    22             }
    23         }
    24     }
    25 }
    Default.aspx.cs
    • 在此实例中涉及到数据存储,但为了快捷方便,直接向对象存储在内存中。在此是利用类文件GuestReponse.cs完成,并且简单的利用数据模型类应用特性来实现验证。
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.ComponentModel.DataAnnotations;
     6 
     7 /// <summary>
     8 /// GuestReponse 的摘要说明
     9 /// </summary>
    10 namespace PartyInvites
    11 {
    12     public class GuestReponse
    13     {
    14         [Required]
    15         public string Name { get; set; }
    16         [Required]
    17         public string Email { get; set; }
    18         [Required]
    19         public string Phone { get; set; }
    20         [Required(ErrorMessage="Please tell us if you will attend")]
    21         public bool? WillAttend { get; set; }
    22     }
    23 }
    GuestReponse.cs
    • ResponseRepository.cs读取实例中的所有数据对象,并向其中添加新对象。
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 
     6 /// <summary>
     7 /// ResponseRepository 的摘要说明
     8 /// </summary>
     9 namespace PartyInvites
    10 {
    11     public class ResponseRepository
    12     {
    13         private static ResponseRepository repository = new ResponseRepository();
    14         private List<GuestReponse> responses = new List<GuestReponse>();
    15 
    16         public static ResponseRepository GetRepository()
    17         {
    18             return repository;
    19         }
    20         public IEnumerable<GuestReponse> GetAllResponses()
    21         {
    22             return responses;
    23         }
    24         public void AddResponse(GuestReponse response) 
    25         {
    26             responses.Add(response);
    27         }
    28         
    29     }
    30 }
    ResponseRepository.cs
    • 实例中另一重要文件Summary.aspx以及其代码隐藏文件Summary.aspx.cs将参加聚会与不参加聚会的人员信息做出总结,两个表格的数据调用方式有些区别,前者直接在页面中包含代码,后者将aspx.cs中所含调用方法的结果利用<%=和%>插入到发送给浏览器的输出中。
     1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Summary.aspx.cs" Inherits="PartyInvites.Summary" %>
     2 <%@ Import Namespace="PartyInvites" %>
     3 <!DOCTYPE html>
     4 
     5 <html xmlns="http://www.w3.org/1999/xhtml">
     6 <head runat="server">
     7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
     8     <title></title>
     9     <link rel="stylesheet" href="StyleSheet.css" />
    10 </head>
    11 <body>
    12     <h2>RSVP Summary</h2>
    13 
    14     <h3>People Who Will Attend</h3>
    15     <table>
    16         <thead>
    17             <tr><th>Name</th><th>Email</th><th>Phone</th></tr>
    18         </thead>
    19         <tbody>
    20             <% var yesData = ResponseRepository.GetRepository().GetAllResponses().Where(r => r.WillAttend.HasValue && r.WillAttend.Value);
    21                foreach (var rsvp in yesData){
    22                    string htmlString = String.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>", rsvp.Name, rsvp.Email, rsvp.Phone);
    23                    Response.Write(htmlString);
    24                }
    25                 %>
    26         </tbody>
    27     </table>
    28    <h3>People Who Will Not Attend</h3>
    29    <table>
    30        <thead>
    31            <tr><th>Name</th><th>Name</th><th>Phone</th></tr>
    32        </thead>
    33        <tbody>
    34            <%= GetNoShowHtml() %>
    35        </tbody>
    36    </table>
    37 </body>
    38 </html>
    Summary.aspx
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.UI;
     6 using System.Web.UI.WebControls;
     7 using System.Text;
     8 
     9 namespace PartyInvites
    10 {
    11     public partial class Summary : System.Web.UI.Page
    12     {
    13         protected void Page_Load(object sender, EventArgs e)
    14         {
    15 
    16         }
    17         protected string GetNoShowHtml() {
    18             StringBuilder html = new StringBuilder();
    19             var noData = ResponseRepository.GetRepository().GetAllResponses().Where(r => r.WillAttend.HasValue && !r.WillAttend.Value);
    20             foreach(var rsvp in noData){
    21                 html.Append(String.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>", rsvp.Name, rsvp.Email, rsvp.Phone));  
    22             }
    23             return html.ToString();
    24         }
    25     }
    26 }
    Summary.aspx.cs
  • 相关阅读:
    R语言:提取路径中的文件名字符串(basename函数)
    课程一(Neural Networks and Deep Learning),第三周(Shallow neural networks)—— 0、学习目标
    numpy.squeeze()的用法
    课程一(Neural Networks and Deep Learning),第二周(Basics of Neural Network programming)—— 4、Logistic Regression with a Neural Network mindset
    Python numpy 中 keepdims 的含义
    课程一(Neural Networks and Deep Learning),第二周(Basics of Neural Network programming)—— 3、Python Basics with numpy (optional)
    课程一(Neural Networks and Deep Learning),第二周(Basics of Neural Network programming)—— 2、编程作业常见问题与答案(Programming Assignment FAQ)
    课程一(Neural Networks and Deep Learning),第二周(Basics of Neural Network programming)—— 0、学习目标
    课程一(Neural Networks and Deep Learning),第一周(Introduction to Deep Learning)—— 0、学习目标
    windows系统numpy的下载与安装教程
  • 原文地址:https://www.cnblogs.com/hhccdf/p/5218135.html
Copyright © 2011-2022 走看看