zoukankan      html  css  js  c++  java
  • asp.net 6.aspx页面

    1.aspx页面的头部

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserInfoList.aspx.cs"  
        Inherits="CZBK.ItcastProject.WebApp.aspx_Demo.UserInfoList" %>
    <%@ Import Namespace="CZBK.ItcastProject.Model" %>

    **<%@ Page %>:页面本身也是一个对象,继承自Page,而<%@ Page %> 就是设置这个对象的属性用的。

    **Language:C#语言

    **AutoEventWireup:启动页面事件

    **CodeBehind:代码后置,代码分离

    **Inherits:继承。 aspx文件会生成一个(子类)类继承于 aspx.cs文件生成的(父类)类。

    **<%@ Import Namespace="CZBK.ItcastProject.Model" %>:引用命名空间

    2.aspx 和 aspx.cs

    aspx文件会生成一个子类,aspx.cs文件会生成一个父类。aspx继承于aspx.cs

    *在aspx.cs文件中生成一个公共属性,aspx文件可以访问到!

    *在aspx.cs页面中使用C#代码,引用命名空间:<%@ Import Namespace="CZBK.ItcastProject.Model" %>;然后<%=  %> 编写C#代码,‘=’ 是response.write

    UserInfoList.aspx.cs

    using CZBK.ItcastProject.Model;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace CZBK.ItcastProject.WebApp.aspx_Demo
    {
        public partial class UserInfoList : System.Web.UI.Page
        {
            public string StrHtml { get; set; }
            public List<UserInfo> UserList { get; set; }
    
            /// <summary>
            /// 页面加载完成以后。Load事件  Form_Load
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
        }
    }

    UserInfoList.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserInfoList.aspx.cs"  Inherits="CZBK.ItcastProject.WebApp.aspx_Demo.UserInfoList" %>
    <%@ Import Namespace="CZBK.ItcastProject.Model" %>
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
        <link href="../Css/tableStyle.css" rel="stylesheet" />
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
         <table> <tr><th>编号</th><th>用户名</th><th>密码</th><th>邮箱</th><th>时间</th><th>删除</th><th>详细</th><th>编辑</th></tr>
                <%=StrHtml%>
                <% foreach(UserInfo userInfo in UserList){%>
                <tr>
                    <td><%=userInfo.Id %></td>
                       <td><%=userInfo.UserName %></td>
                       <td><%=userInfo.UserPass %></td>
                       <td><%=userInfo.Email %></td>
                       <td><%=userInfo.RegTime.ToShortDateString() %></td>
                    <td><a href="Delete.ashx?id=<%=userInfo.Id %>" class="deletes">删除</a></td>
                    <td>详细</td>
                    <td><a href="EditUser.aspx?id=<%=userInfo.Id %>">编辑</a> </td>
                </tr>
    
                <%} %>
            </table>
            <hr />
        </div>
        </form>
    </body>
    </html>

    3.Page_Load 方法 

    *页面加载完成触发(服务端的)

    *aspx.cs中的 Page_Load 方法比aspx中的  window.onload = function () {} 方法要先执行!

    4.aspx列表展示数据

    UserInfoList.aspx:

    1.引用命名空间

    2.遍历父类的userInfo(list集合) 公共属性,循环生成一行tr

    3.注意html代码可以结合C#代码编写在aspx中

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserInfoList.aspx.cs"  Inherits="CZBK.ItcastProject.WebApp.aspx_Demo.UserInfoList" %>
    <%@ Import Namespace="CZBK.ItcastProject.Model" %>
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
        <link href="../Css/tableStyle.css" rel="stylesheet" />
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
         <table> <tr><th>编号</th><th>用户名</th><th>密码</th><th>邮箱</th><th>时间</th><th>删除</th><th>详细</th><th>编辑</th></tr>
                <% foreach(UserInfo userInfo in UserList){%>
                <tr>
                    <td><%=userInfo.Id %></td>
                       <td><%=userInfo.UserName %></td>
                       <td><%=userInfo.UserPass %></td>
                       <td><%=userInfo.Email %></td>
                       <td><%=userInfo.RegTime.ToShortDateString() %></td>
                    <td><a href="Delete.ashx?id=<%=userInfo.Id %>" class="deletes">删除</a></td>
                    <td>详细</td>
                    <td><a href="EditUser.aspx?id=<%=userInfo.Id %>">编辑</a> </td>
                </tr>
                <%} %>
            </table>
            <hr />
        </div>
        </form>
    </body>
    </html>

     UserInfoList.aspx.cs

    1.定义公共属性,以便子类访问

    2.调用业务层的方法

    using CZBK.ItcastProject.Model;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace CZBK.ItcastProject.WebApp.aspx_Demo
    {
        public partial class UserInfoList : System.Web.UI.Page
        {
            public string StrHtml { get; set; }
            public List<UserInfo> UserList { get; set; }
    
            /// <summary>
            /// 页面加载完成以后。Load事件  Form_Load
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void Page_Load(object sender, EventArgs e)
            {
                BLL.UserInfoService UserInfoService = new BLL.UserInfoService();
                List<UserInfo> list = UserInfoService.GetList();
                UserList = list;
            }
        }
    }

    5.删除数据

    采用一般处理程序,因为删除不需要展示复杂的页面,这里用一般处理程序比aspx方便。

    前台的删除代码

    <td><a href="Delete.ashx?id=<%=userInfo.Id %>" class="deletes">删除</a></td>

    Delete.ashx

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace CZBK.ItcastProject.WebApp.aspx_Demo
    {
        /// <summary>
        /// Delete 的摘要说明
        /// </summary>
        public class Delete : IHttpHandler
        {
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                BLL.UserInfoService UserInfoService = CZBK.ItcastProject.Common.CacheControl.Get<BLL.UserInfoService>();
                int id;
                if(int.TryParse(context.Request.QueryString["id"],out id))
                {
                    if (UserInfoService.DeleteUserInfo(id))
                    {
                        context.Response.Redirect("UserInfoList.aspx");
                    }
                    else
                    {
                        context.Response.Redirect("Error.html");
                    }
                }
                else
                {
                    context.Response.Redirect("Error.html");
                }
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }

    6.添加数据

    1. runat="server" 服务端控件! 会被.netframework解析成客户端的HTML代码返回给浏览器!但是会自动生成一个隐藏域,name="__ViewState"!这个ViewState用于保持状态连接,存储相关数据!

    ***IsPostBack:是根据__VIEWSTATE隐藏域进行判断的!

    ***如果不添加runat="server",则不会自动生成这个隐藏域,name="__ViewState"

    2.aspx页面可以接受Get请求或者Post请求!注意:无论是Get请求还是Post请求,都会访问aspx.cs类中的Page_Load方法!!

    3.aspx页面区分Get和Post请求:

       a.在form表单中添加一个隐藏域,判断隐藏域的值;aspx.cs类中的Page_load方法中判断隐藏域的值,如果有值说明表单提交,是Post请求,否则为Get请求

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddUserInfo.aspx.cs" 
        Inherits="CZBK.ItcastProject.WebApp.aspx_Demo.AddUserInfo" %>
    
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" method="post" action="AddUserInfo.aspx" >
        <div>
            用户名:<input type="text" name="txtName" /><br />
            密码:<input type="password" name="txtPwd" /><br />
            邮箱:<input type="text" name="txtMail" /><br />
            <input type="hidden" name="isPostBack" value="aaa" /><!--隐藏域的值用于判断Post请求-->
            <input type="submit" value="添加用户" />
        </div>
        </form>
    </body>
    </html>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using CZBK.ItcastProject;
    
    namespace CZBK.ItcastProject.WebApp.aspx_Demo
    {
        public partial class AddUserInfo : System.Web.UI.Page
        {
            BLL.UserInfoService UserInfoService = new BLL.UserInfoService();
            protected void Page_Load(object sender, EventArgs e)
            {
                //判断 Get,Post请求
                //如果隐藏域的值不为空,表示用户单击了提交按钮发出了POST请求
                if(!string.IsNullOrEmpty(Request.Form["isPostBack"]))
                {
                    //post请求
                    AddUserInfo_Method();
                }
                else
                {
                    //get请求
                }
            }
    
            private void AddUserInfo_Method()
            {
                Model.UserInfo userinfo = new Model.UserInfo();
                userinfo.UserName = Request.Form["txtName"];
                userinfo.UserPass = Request.Form["txtPwd"];
                userinfo.Email = Request.Form["txtMail"];
                userinfo.RegTime = DateTime.Now;
                if (UserInfoService.AddUserInfo(userinfo))
                {
                    Response.Redirect("UserInfoList.aspx");
                }
                else
                {
                    Response.Redirect("../Error.html");
                }
            }
        }
    }

       

       b.使用服务端的 form表单,runat="server"!可以用IsPostBack来判断是否为Post请求还是Get请求!!!

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddUserInfo.aspx.cs" 
        Inherits="CZBK.ItcastProject.WebApp.aspx_Demo.AddUserInfo" %>
    
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server" >
        <div>
            用户名:<input type="text" name="txtName" /><br />
            密码:<input type="password" name="txtPwd" /><br />
            邮箱:<input type="text" name="txtMail" /><br />
            <input type="hidden" name="isPostBack" value="aaa" /><!--隐藏域的值用于判断Post请求-->
            <input type="submit" value="添加用户" />
        </div>
        </form>
    </body>
    </html>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using CZBK.ItcastProject;
    
    namespace CZBK.ItcastProject.WebApp.aspx_Demo
    {
        public partial class AddUserInfo : System.Web.UI.Page
        {
            BLL.UserInfoService UserInfoService = new BLL.UserInfoService();
            protected void Page_Load(object sender, EventArgs e)
            {
                //判断 Get,Post请求
                //IsPostBack:如果是POST请求该属性的值为true,如果是GET请求该属性的值为false.
    
                //IsPostBack:是根据__VIEWSTATE隐藏域进行判断的,如果是POST请求那么该隐藏域的值会提交到服务端,
                //那么IsPostBack属性也就为true.
                //如果将form标签的runat="server"去掉,那么就不能用该属性进行判断是POST请求还是GET请求。
                //因为去掉form标签的runat="server",那么就不会再产生 __VIEWSTATE隐藏域了。
                if(IsPostBack)
                {
                    AddUserInfo_Method();
                }
            }
    
            private void AddUserInfo_Method()
            {
                Model.UserInfo userinfo = new Model.UserInfo();
                userinfo.UserName = Request.Form["txtName"];
                userinfo.UserPass = Request.Form["txtPwd"];
                userinfo.Email = Request.Form["txtMail"];
                userinfo.RegTime = DateTime.Now;
                if (UserInfoService.AddUserInfo(userinfo))
                {
                    Response.Redirect("UserInfoList.aspx");
                }
                else
                {
                    Response.Redirect("../Error.html");
                }
            }
        }
    }

    7.更新数据

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EditUser.aspx.cs" 
        Inherits="CZBK.ItcastProject.WebApp.aspx_Demo.EditUser" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" method="post" action="EditUser.aspx">
        <div>
         用户名:<input type="text" name="txtName" value="<%=EditUserInfo.UserName %>" /><br />
            密码:<input type="text" name="txtPwd" value="<%=EditUserInfo.UserPass %>" /><br />
            邮箱:<input type="text" name="txtMail" value="<%=EditUserInfo.Email %>" /><br />
            <input type="hidden" name="txtId" value="<%=EditUserInfo.Id %>" />
            <input type="hidden" name="txtRegTime" value="<%=EditUserInfo.RegTime %>" />
            <input type="hidden" name="Ispostback" value="aaa" />
            <input type="submit" value="编辑用户" />
        </div>
        </form>
    </body>
    </html>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using CZBK.ItcastProject;
    
    namespace CZBK.ItcastProject.WebApp.aspx_Demo
    {
        public partial class EditUser : System.Web.UI.Page
        {
            BLL.UserInfoService userInfoservice = Common.CacheControl.Get<BLL.UserInfoService>();
            public Model.UserInfo EditUserInfo { get; set; }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!string.IsNullOrEmpty(Request.Form["Ispostback"]))
                {
                    EditMethod();//post update
                }
                else
                {
                    GetUserInfoMethod();//get 
                }
            }
            private void GetUserInfoMethod()
            {
                int id;
                if (int.TryParse(Request.QueryString["id"], out id))
                {
                    Model.UserInfo userinfo = userInfoservice.GetDeail(id);
                    if (userinfo != null)
                    {
                        EditUserInfo = userinfo;
                    }
                    else
                        Response.Redirect("../Error.html");
                }
            }
            private void EditMethod()
            {
                Model.UserInfo userinfo = new Model.UserInfo();
                userinfo.UserName = Request.Form["txtName"];
                userinfo.UserPass = Request.Form["txtPwd"];
                userinfo.Email = Request.Form["txtMail"];
                userinfo.Id = Convert.ToInt32(Request.Form["txtId"]);
                userinfo.RegTime = DateTime.Now;
                if (userInfoservice.UpdateUserInfo(userinfo))
                {
                    Response.Redirect("UserInfoList.aspx");
                }
                else
                    Response.Redirect("../Error.html");
            }
        }
    }
  • 相关阅读:
    2012 regional 。。
    joj2443
    java笔记代码实现汉诺塔移动过程和移动次数
    java笔记增加虚拟机内存
    java笔记修改javadoc为中文API信息
    java笔记String类格式化当天日期转换符文档
    java笔记正则表达式的运用(包括电话,邮箱验证等)
    java笔记String类对象解析与运用
    Java笔记IO流的运用
    java笔记BigDecimal的使用
  • 原文地址:https://www.cnblogs.com/youguess/p/9264154.html
Copyright © 2011-2022 走看看