案例:个人信息管理。
l 使用NVelocity的开发方式重写登录程序,把NVelocity封装成
RenderTemplate方法。
l 这种HttpHandler+ NVelocity的方式非常类似于PHP+smarty的开
发方式,也有利于理解asp.net mvc。HttpHandler就是Controller
,模板就是View, Controller不操作View的内部细节,只是把数
据给View,由View自己控制怎样显示。
l 字段:Id、姓名、年龄、个人网址(可以为空)。
l 列表页面和编辑界面:PersonList.aspx、
PersonEdit.aspx?Action=AddNew、
PersonEdit.aspx?Action=Edit&Id=2(在PersonEdit页面判断是
否传过来了save按钮来判断是加载还是保存。渲染页面的时候把
Action和Id保存在隐藏字段中。保存成功后Redirect回List页面)
l 进一步案例:有关联字段,比如班级,实现备注
using System; using System.Collections.Generic; using System.Linq; using System.Web; using NVelocity.App; using NVelocity.Runtime; using NVelocity; namespace CRUDTest1 { public class CommonHelper { /// <summary> /// 用data数据填充templateName模板,渲染生成html返回 /// </summary> /// <param name="templateName"></param> /// <param name="data"></param> /// <returns></returns> public static string RenderHtml(string templateName, object data) { VelocityEngine vltEngine = new VelocityEngine(); vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file"); vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹 vltEngine.Init(); VelocityContext vltContext = new VelocityContext(); vltContext.Put("Data", data);//设置参数,在模板中可以通过$data来引用 Template vltTemplate = vltEngine.GetTemplate(templateName); System.IO.StringWriter vltWriter = new System.IO.StringWriter(); vltTemplate.Merge(vltContext, vltWriter); string html = vltWriter.GetStringBuilder().ToString(); return html; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; using System.Data.SqlClient; using System.Data; namespace CRUDTest1 { public static class SqlHelper { public static readonly string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString; public static SqlConnection OpenConnection() { SqlConnection conn = new SqlConnection(connstr); conn.Open(); return conn; } public static int ExecuteNonQuery(string cmdText, params SqlParameter[] parameters) { using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); return ExecuteNonQuery(conn, cmdText, parameters); } } public static object ExecuteScalar(string cmdText, params SqlParameter[] parameters) { using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); return ExecuteScalar(conn, cmdText, parameters); } } public static DataTable ExecuteDataTable(string cmdText, params SqlParameter[] parameters) { using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); return ExecuteDataTable(conn, cmdText, parameters); } } public static int ExecuteNonQuery(SqlConnection conn,string cmdText, params SqlParameter[] parameters) { using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = cmdText; cmd.Parameters.AddRange(parameters); return cmd.ExecuteNonQuery(); } } public static object ExecuteScalar(SqlConnection conn, string cmdText, params SqlParameter[] parameters) { using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = cmdText; cmd.Parameters.AddRange(parameters); return cmd.ExecuteScalar(); } } public static DataTable ExecuteDataTable(SqlConnection conn, string cmdText, params SqlParameter[] parameters) { using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = cmdText; cmd.Parameters.AddRange(parameters); using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)) { DataTable dt = new DataTable(); adapter.Fill(dt); return dt; } } } public static object ToDBValue(this object value) { return value == null ? DBNull.Value : value; } public static object FromDBValue(this object dbValue) { return dbValue == DBNull.Value ? null : dbValue; } } }
<?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应用程序的详细消息,请访问 http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <connectionStrings> <add name="connstr" connectionString="Data Source=.;Initial Catalog=CRUDTest;User Id=sa;Password=123456;"></add> </connectionStrings> </configuration>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; namespace CRUDTest1 { /// <summary> /// PersonList 的摘要说明 /// </summary> public class PersonList : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html"; DataTable dt = SqlHelper.ExecuteDataTable("select * from T_Persons"); //DataTable不是集合,所以无法foreach遍历,DataTable的Rows属性 //代表表格中的数据行的集合(DataRow的集合),一般传递DataRowCollection //给模板方便遍历 string html = CommonHelper.RenderHtml("PersonList.htm", dt.Rows); context.Response.Write(html); //MVC: } public bool IsReusable { get { return false; } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.SqlClient; using System.Data; namespace CRUDTest1 { /// <summary> /// PersonEdit 的摘要说明 /// </summary> public class PersonEdit : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html"; //PersonEdit.ashx?action=AddNew //PersonEdit.ashx?action=Edit&Id=3 string action = context.Request["Action"]; if (action == "AddNew") { //判断是否含有Save并且等于true,如果是的话就说明是点击【保存】按钮请求来的 bool save = Convert.ToBoolean(context.Request["Save"]); if (save)//是保存 { string name = context.Request["Name"]; int age = Convert.ToInt32(context.Request["Age"]); string email = context.Request["Email"]; SqlHelper.ExecuteNonQuery("Insert into T_Persons(Name,Age,Email) values(@Name,@Age,@Email)", new SqlParameter("@Name", name) , new SqlParameter("@Age", age) , new SqlParameter("@Email", email)); context.Response.Redirect("PersonList.ashx");//保存成功返回列表页面 } else { //string html = CommonHelper.RenderHtml("PersonEdit.htm", new { Name = "", Age = 20, Email = "@rupeng.com" }); //var data = new { Name = "", Age = 20, Email = "@rupeng.com" }; var data = new { Action = "AddNew", Person = new { Name = "", Age = 20, Email = "@rupeng.com" } }; string html = CommonHelper.RenderHtml("PersonEdit.htm", data); context.Response.Write(html); } } else if (action == "Edit") { bool save = Convert.ToBoolean(context.Request["Save"]); if (save) { string name = context.Request["Name"]; int age = Convert.ToInt32(context.Request["Age"]); string email = context.Request["Email"]; long id = Convert.ToInt64(context.Request["id"]); SqlHelper.ExecuteNonQuery("update T_Persons set [email protected],[email protected],[email protected] where [email protected]", new SqlParameter("@Name", name) , new SqlParameter("@Age", age) , new SqlParameter("@Email", email) , new SqlParameter("@Id", id)); context.Response.Redirect("PersonList.ashx");//保存成功返回列表页面 } else { long id = Convert.ToInt64(context.Request["Id"]); DataTable dt = SqlHelper.ExecuteDataTable("select * from T_Persons where [email protected]", new SqlParameter("@Id", id)); if (dt == null || dt.Rows == null || dt.Rows.Count <= 0) { context.Response.Write("没有找到Id=" + id + "的数据"); return; } else if (dt.Rows.Count > 1) { context.Response.Write("找到多条Id=" + id + "的数据"); } else { DataRow row = dt.Rows[0]; var data = new { Action = "Edit", Person = row }; string html = CommonHelper.RenderHtml("PersonEdit.htm", data); context.Response.Write(html); } } } else if (action == "Delete") { long id = Convert.ToInt64(context.Request["Id"]); SqlHelper.ExecuteNonQuery("delete from T_Persons where [email protected]", new SqlParameter("@Id", id)); context.Response.Redirect("PersonList.ashx"); } else { context.Response.Write("Action参数错误!"); } } public bool IsReusable { get { return false; } } } }
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>人员列表</title> </head> <body> <a href="PersonEdit.ashx?Action=AddNew">新增人员</a> <table> <thead> <tr><td>编辑</td><td>删除</td><td>姓名</td><td>年龄</td><td>邮箱</td></tr> </thead> <tbody> #foreach($person in $Data) <tr><td><a href="PersonEdit.ashx?Action=Edit&Id=$person.Id">编辑</a></td><td><a href="PersonEdit.ashx?Action=Delete&Id=$person.Id">删除</a></td><td>$person.Name</td><td>$person.Age</td><td>$person.Email</td></tr> #end </tbody> </table> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> #if($Data.Action="AddNew") 新增用户 #else 编辑用户$Data.Person.Name #end </title> </head> <body> <form action="PersonEdit.ashx" method="post"> <input type="hidden" name="Action" value="$Data.Action" /> <input type="hidden" name="Save" value="true" /> <input type="hidden" name="Id" value="$Data.Person.Id" /> <table> <tr><td>姓名:</td><td><input type="text" name="Name" value="$Data.Person.Name" /></td></tr> <tr><td>年龄:</td><td><input type="text" name="Age" value="$Data.Person.Age" /></td></tr> <tr><td>邮箱:</td><td><input type="text" name="Email" value="$Data.Person.Email" /></td></tr> <tr><td></td><td><input type="submit" value="保存"/></td></tr> </table> </form> </body> </html>
时间: 2024-10-14 14:37:55