7/7 在ASP.NET Web应用程序中应用Nello框架示例
来源:互联网 发布:php百度地图api 定位 编辑:程序博客网 时间:2024/05/18 03:09
1、样例应用的需求描述
以企业商品管理为例,通常商品管理模块需要实现的功能有商品列表的查询、商品的新增、商品信息的修改、商品的删除等功能,而商品通常包含的属性有商品编号、商品名称、规格型号、单价、计量单位等。
2、样例应用的环境准备
在实现该样例应用之前,必须搭建一个必要的环境以支持样例的开发和运行,可以将开发和运行的环境配置在同一台机器上。
1)服务器环境
安装Microsoft .NET Framework 2.0和Internet 信息服务(IIS)。
2)数据库环境
安装Microsoft SQL Server 2000。
3)开发工具准备
安装Microsoft Visual Studio 2005。
3、样例应用的实现
1)建立WEB应用程序
打开Microsoft Visual Studio 2005,在菜单中选择[文件]—[新建]—[网站],建立网站D:/WebProduct,如图
图
添加Web窗体ProductList.aspx用于显示商品列表,添加Web窗体ProductEdit.aspx用于商品的新增和修改,添加类BProduct.cs用于实现业务层逻辑。完成后如图
图
2)添加对Nello框架类库的引用
图
成功添加引用后即可在工具箱看到Nello框架的相关控件,如图
图
3)建立数据库
打开Microsoft SQL Server 2000 企业管理器,新建数据库sampledb,并在其中新建商品表t_product,如图
图
相关的脚本为:
--创建数据库
CREATE DATABASE [sampledb] ON (NAME = N'sampledb_Data', FILENAME = N'C:/Program Files/Microsoft SQL Server/MSSQL/data/sampledb_Data.MDF' , SIZE = 1, FILEGROWTH = 10%)
COLLATE Chinese_PRC_CI_AS
GO
--创建表
CREATE TABLE [t_product] (
[fguid] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[fcode] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[fname] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[ftype] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[fprice] [numeric](18, 2) NULL ,
[funit] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
CONSTRAINT [PK_t_product] PRIMARY KEY CLUSTERED
(
[fguid]
) ON [PRIMARY]
) ON [PRIMARY]
GO
4)设置数据库连接
Nello框架提供了两种方法来设置数据库连接:
其一是直接从WEB应用程序的配置文件web.config的配置项appSettings里直接读取,如:
<appSettings>
<add key="ClassFactory.ConnectionType" value="AppSettings.ConnectionString"/>
<add key="ConnectionString" value="Data Source=localhost;Initial Catalog=sampledb;User ID=sa;Password=p@ssw0rd;"/>
</appSettings>
其二是从实现了IconnectionInfo接口的类文件里面读取,如:
<appSettings>
<add key="ClassFactory.ConnectionType" value="Class.Nello!Nello.Web.ConnectionInfo"/>
<add key="ClassFactory.ConnectionType" value="Class.ConnectionInfo"/>
</appSettings>
若指定了程序集,则从指定程序集中查找,若未指定,则从AppCode目录所在的程序集中查找。
除了数据库连接方式外,一般还需要设置数据提供者:
<add key="ClassFactory.ProviderName" value="System.Data.SqlClient"/>
5)表示层设计与实现
表示层由ProductEdit.aspx和ProductList.aspx组成,具体实现如下:
ProductList.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ProductList.aspx.cs" Inherits="ProductList" %>
<%@ Register assembly="Nello" namespace="Nello.Web.Controls" tagprefix="nello" %>
<!DOCTYPE html PUBLIC "-//W
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>商品列表</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnAdd" runat="server" Text="新 增" onclick="btnAdd_Click" />
<asp:Button ID="btnEdit" runat="server" Text="修 改" onclick="btnEdit_Click" />
<asp:Button ID="btnDelete" runat="server" Text="删 除" onclick="btnDelete_Click" OnClientClick="if(!confirm('确定要删除当前记录吗?')) return false;" />
<nello:ModalDialog ID="ModalDialog1" runat="server"
onreturn="ModalDialog1_Return" />
<br />
<nello:NGridView ID="gridProduct" runat="server" AutoGenerateColumns="False"
FullRowSelect="True" HideFirstColumn="True" LimitCellChars="True"
MaxCellChars="15">
<Columns>
<asp:BoundField DataField="fguid" HeaderText="fguid" />
<asp:BoundField DataField="fcode" HeaderText="商品编号" />
<asp:BoundField DataField="fname" HeaderText="商品名称" />
<asp:BoundField DataField="ftype" HeaderText="规格型号" />
<asp:BoundField DataField="fprice" HeaderText="单价" />
<asp:BoundField DataField="funit" HeaderText="计量单位" />
</Columns>
<SelectedRowStyle BackColor="RoyalBlue" ForeColor="White"></SelectedRowStyle>
<HeaderStyle BackColor="SteelBlue" ForeColor="White"></HeaderStyle>
</nello:NGridView>
</div>
</form>
</body>
</html>
ProductEdit.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ProductEdit.aspx.cs" Inherits="ProductEdit" %>
<%@ Register assembly="Nello" namespace="Nello.Web.Controls" tagprefix="nello" %>
<!DOCTYPE html PUBLIC "-//W
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<base target="_self" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnSave" runat="server" Text="保存" onclick="btnSave_Click" />
<asp:Button ID="btnCancel" runat="server" Text="取消" onclick="btnCancel_Click" />
<br />
<br />
<table>
<tr style="display:none">
<td>fguid</td>
<td>
<nello:NCommand ID="fguid" runat="server" DataType="String" IsIgnore="False" Visible="False">
</nello:NCommand>
</td>
</tr>
<tr>
<td>商品编号</td>
<td>
<nello:NTextBox ID="fcode" runat="server" />
</td>
</tr>
<tr>
<td>商品名称</td>
<td>
<nello:NTextBox ID="fname" runat="server" />
</td>
</tr>
<tr>
<td>规格型号</td>
<td>
<nello:NTextBox ID="ftype" runat="server" />
</td>
</tr>
<tr>
<td>单价</td>
<td>
<nello:NTextBox ID="fprice" runat="server" DataType="Numeric" />
</td>
</tr>
<tr>
<td>
计量单位</td>
<td>
<nello:NDropDownList ID="funit" runat="server" DataType="String" Height="21px" Width="100px">
<asp:ListItem>米</asp:ListItem>
<asp:ListItem>公斤</asp:ListItem>
<asp:ListItem>升</asp:ListItem>
<asp:ListItem>公里</asp:ListItem>
</nello:NDropDownList>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
6)控制层设计与实现
控制层由ProductEdit.aspx.cs和ProductList.aspx.cs组成,具体实现如下:
ProductList.aspx.cs
using System;
using System.Data;
using Nello.Web;
public partial class ProductList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//页面首次加载时显示数据库中的数据
if (!IsPostBack)
{
DoDataBind();
}
}
//执行数据绑定显示数据库中的数据
private void DoDataBind()
{
DataTable dt = BProduct.GetItems();
gridProduct.DoDataBind(dt);
}
//点击新增按钮打开新增商品对话框
protected void btnAdd_Click(object sender, EventArgs e)
{
ModalDialog1.OpenAdd("ProductEdit.aspx");
}
//点击修改按钮打开修改商品对话框
protected void btnEdit_Click(object sender, EventArgs e)
{
if (gridProduct.SelectedIndex < 0)
{
JavaScript.Alert("请选择要修改的记录!");
return;
}
string guid = gridProduct.SelectedRow.Cells[0].Text;
ModalDialog1.AddQueryString("itemid", guid);
ModalDialog1.OpenEdit("ProductEdit.aspx");
}
//点击删除按钮执行商品删除并刷新商品列表
protected void btnDelete_Click(object sender, EventArgs e)
{
if (gridProduct.SelectedIndex < 0)
{
JavaScript.Alert("请选择要删除的记录!");
return;
}
string guid = gridProduct.SelectedRow.Cells[0].Text;
BProduct.Delete(guid);
DoDataBind();
}
//新增或修改商品对话框返回时刷新商品列表
protected void ModalDialog1_Return(string ReturnText)
{
DoDataBind();
}
}
ProductEdit.aspx.cs
using System;
using System.Collections;
using System.Data;
using Nello;
using Nello.Web;
public partial class ProductEdit : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//根据NCommand控件fguid的状态决定如何初始化页面
if (fguid.IsAdd)
{
this.Title = "商品新增";
fguid.Text = Util.NewGuid();
}
else
{
this.Title = "商品修改";
string itemid = Request.QueryString["itemid"];
DataTable dt = BProduct.GetItemById(itemid);
PageUtil.ExchangeData(this, dt); //将从数据库获取的数据传输至页面
}
}
}
//关闭模式对话框
protected void btnCancel_Click(object sender, EventArgs e)
{
JavaScript.CloseWindow();
}
//根据NCommand控件fguid的状态决定如何保存数据
protected void btnSave_Click(object sender, EventArgs e)
{
Hashtable ht = new Hashtable();
PageUtil.ExchangeData(ht, this); //将从页面获取的数据传输至哈希表
if (fguid.IsAdd)
{
BProduct.Add(ht);
}
else
{
BProduct.Modify(ht);
}
//当不需要从模式对话框返回值时可返回Util.NewGuid()以确保ModalDialog控件的Return事件被触发
Nello.Web.Controls.ModalDialog.ReturnBack(this, Util.NewGuid());
}
}
7)业务层设计与实现
业务层在类BProduct中实现,如下:
using System.Collections;
using System.Data;
using Nello.Data;
public class BProduct
{
/// <summary>
/// 获取表内所有记录
/// </summary>
public static DataTable GetItems()
{
return Business.GetItems("t_product");
}
/// <summary>
/// 根据记录ID获取一行记录
/// </summary>
/// <param name="guid">指定的记录ID</param>
public static DataTable GetItemById(string guid)
{
return Business.GetItem("t_product","fguid",guid);
}
/// <summary>
/// 新增记录
/// </summary>
/// <param name="ht">包含数据表的字段和值的哈希表</param>
public static int Add(Hashtable ht)
{
return Business.Add(ht, "t_product");
}
/// <summary>
/// 修改记录
/// </summary>
/// <param name="ht">包含数据表的字段和值的哈希表</param>
public static int Modify(Hashtable ht)
{
return Business.Modify(ht, "t_product","fguid");
}
/// <summary>
/// 删除记录
/// </summary>
/// <param name="guid">指定的记录ID</param>
public static int Delete(string guid)
{
return Business.Delete("t_product", "fguid", guid);
}
}
8)完成后的程序运行结果如下:
显示商品列表,如图
图
新增商品,如图
图
修改商品,如图
图
- 7/7 在ASP.NET Web应用程序中应用Nello框架示例
- 0/7 基于ASP.NET的Web应用程序框架Nello的设计与实现
- 1/7 Nello框架简介
- 4/7 Nello框架的特点和应用要求
- Asp.Net中应用Web Service示例
- 2/7 Nello框架的设计目标
- 3/7 Nello框架的目标用户
- 5/7 Nello框架的总体设计
- 在 ASP.NET 2.0 中创建 Web 应用程序主题
- 在 ASP.NET 2.0 中创建 Web 应用程序主题【微软】
- 在 ASP.NET 2.0 中创建 Web 应用程序主题
- 在 ASP.NET 2.0 中创建 Web 应用程序主题
- 在 ASP.NET 2.0 中创建 Web 应用程序主题【微软】
- Asp.Net在Web应用程序中执行计划任务(多线程)
- 在 ASP.NET Web 应用程序中读写 HTTP Cookie
- 在 IIS 6.0 中创建 ASP.NET Web 应用程序根目录
- 在IIS中运行asp.net 5 web应用程序
- 在docker中运行ASP.NET Core Web API应用程序
- 手动修改注册表来解决开机自动打开网页的问题
- Internet 需要多语种
- c++中的类型转换,static_cast,reinterpret_cast,boost::lexical_cast
- C/C++知识点1
- Advanced Linux Sound Architecture (ALSA) 研究笔记
- 7/7 在ASP.NET Web应用程序中应用Nello框架示例
- select 语句的用法(转贴)
- 设计模式—工厂方法
- C语言中可变参数的用法
- 触发器错误引起sql语句执行出错
- TNS-12170 连接超时
- 太粗心
- Symbian如何扩展栈和堆的大小
- FI--125 Questions to study for SAP FI CO Certification