一个简单的ASP.NET Web Forms应用程序
Posted 书香香鳳金閣紫陽
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个简单的ASP.NET Web Forms应用程序相关的知识,希望对你有一定的参考价值。
ASP.NET 支持三种不同的开发模式: Web Pages(Web 页面)、MVC(Model View Controller 模型-视图-控制器)、Web Forms(Web 窗体)
我给大家展示的是Web Forms(Web 窗体)
第一:我们需要了解下,什么是Web Forms。
Web Forms 是三种创建 ASP.NET 网站和 Web 应用程序的编程模式中的一种。
Web Forms 是最古老的 ASP.NET 编程模式,是整合了 html、服务器控件和服务器代码的事件驱动网页。
Web Forms 是在服务器上编译和执行的,再由服务器生成 HTML 显示为网页。
Web Forms 有数以百计的 Web 控件和 Web 组件用来创建带有数据访问的用户驱动网站。
详情请查阅:http://www.runoob.com/aspnet/aspnet-intro.html
第二:Web Forms 的搭建
废话就不多说了,还是来直接操作吧。
步骤一:确定你的开发环境:vs2015 sql2008。
步骤二:凡事开发都离不开数据库的搭建,搭建你的数据库,插入一条数据,查看是否有数据,这我就不废话了。
-- 用户表 drop table [User] Create Table [User] ( "Theserialnumber" int IDENTITY (1, 1) NOT NULL , "UserID" varchar(50) NOT NULL , "UserName" varchar(30), "UserSet" char(2), "Userphone" char(11), "UserworkID" varchar(50), "UserlevelID" varchar(20), "UserTypeID" varchar(50), "UserCreationtime" dateTime, "UserhobbyID" varchar(50), ) alter table [User] add constraint pk_name primary key ("Theserialnumber","UserID"); --用户工作表 drop table Work Create Table Work ( "UserworkID" varchar(50) Primary Key NOT NULL, "UserworkType" varchar(50), "WorkDetailsID" varchar(50) ) --用户工作详情表 drop table WorkDetails Create Table WorkDetails ( "WorkDetailsID" varchar(50) Primary Key, "WorkDetailsSituation" varchar(50) ) --权限表 drop table [level] Create Table [level] ( "UserlevelID" varchar(50) Primary Key, "UserleverlType" varchar(50) ) --类型表 drop table UserType Create Table UserType ( "UserTypeID" varchar(50) Primary Key, "UserType" varchar(50) ) --业余表 drop table Userhobby Create Table Userhobby ( "UserhobbyID" varchar(50) Primary Key, "Userhobby" varchar(50) ) --增加数据 insert into [User](UserID,UserName,UserSet,Userphone,UserworkID, UserlevelID,UserTypeID,UserCreationtime,UserhobbyID) values(\'2020020901494949\',\'张三\',\'男\',\'13817475159\',\'1\', \'1\',\'1\',2020/2/9,\'1\') insert into Work(UserworkID, UserworkType, WorkDetailsID)values(\'1\',\'IT\',\'2020020901494949\') insert into WorkDetails(WorkDetailsID, WorkDetailsSituation)values(\'2020020901494949\',\'开发编码\') insert into [level] (UserlevelID, UserleverlType)values(\'1\',\'管理员\') insert into [level] (UserlevelID, UserleverlType)values(\'2\',\'普通用户\') insert into UserType(UserTypeID, UserType)values(\'1\',\'开发人员\') insert into Userhobby(UserhobbyID, Userhobby)values(\'1\',\'编码\') insert into Userhobby(UserhobbyID, Userhobby)values(\'2\',\'吃\') insert into Userhobby(UserhobbyID, Userhobby)values(\'3\',\'学习\') insert into Userhobby(UserhobbyID, Userhobby)values(\'4\',\'工作\') Select Theserialnumber, UserID, UserName, UserSet, Userphone, work.UserworkType,Details.WorkDetailsSituation, [level].UserleverlType,[type].UserType, UserCreationtime, hobby.Userhobby from [User] inner join Work on Work.UserworkID=[User].UserworkID inner join [level] on [level].UserlevelID=[user].UserlevelID inner join UserType as [type] on [type].UserTypeID=[USER].UserTypeID inner join WorkDetails as Details on Details.WorkDetailsID=Work.WorkDetailsID inner join Userhobby as hobby on hobby.UserhobbyID=[user].UserhobbyID
步骤三:打开你的Visual Studio,新建---项目---ASP.NET Web应用程序---空的WebForms应用程序。
步骤四:建一个类库Model(模型)和一个数据访问层DAL
步骤五:在Model建一个ADO.NET实体数据模型来自数据库的Code First
步骤六:建立引用(索引)
DAL索引Model和程序集框架System.Configuration系统配置文件
WebForms应用程序索引DAL和Model
配置好Web.config文件
步骤七:在DAL数据访问层中搭建DBbase类,用来处理模型数据的增删改查
//读取配置文件,连接数据库语句 public static string strCon = System.Configuration.ConfigurationManager.ConnectionStrings["Family"].ConnectionString; //实例化连接对象 con SqlConnection con = new SqlConnection(strCon); //检测连接是否打开 public void Connection() { if (this.con.State == ConnectionState.Closed) { this.con.Open(); } } //根据提供的strSQL语句 访问数据库,返回List集合 public List<User> GetDataSet(string strSQL) { Connection(); try { SqlDataAdapter da = new SqlDataAdapter(strSQL, con); DataSet ds = new DataSet(); da.Fill(ds); List<User> list = new List<User>(); if (ds.Tables.Count > 0) { for (int i = 0; i < ds.Tables.Count; i++) { foreach (DataRow dr in ds.Tables[i].Rows) { User obj = new User(); if (ds.Tables[i].Columns.Contains("UserID")) obj.UserID = Convert.ToString(dr["UserID"]); if (ds.Tables[i].Columns.Contains("UserName")) obj.UserName = Convert.ToString(dr["UserName"]); if (ds.Tables[i].Columns.Contains("UserSet")) obj.UserSet = Convert.ToString(dr["UserSet"]); if (ds.Tables[i].Columns.Contains("Userphone")) obj.Userphone = Convert.ToString(dr["Userphone"]); if (ds.Tables[i].Columns.Contains("UserworkType")) obj.UserworkType = Convert.ToString(dr["UserworkType"]); if (ds.Tables[i].Columns.Contains("WorkDetailsSituation")) obj.WorkDetailsSituation = Convert.ToString(dr["WorkDetailsSituation"]); if (ds.Tables[i].Columns.Contains("UserleverlType")) obj.UserleverlType = Convert.ToString(dr["UserleverlType"]); if (ds.Tables[i].Columns.Contains("UserType")) obj.UserType = Convert.ToString(dr["UserType"]); if (ds.Tables[i].Columns.Contains("UserCreationtime")) obj.UserCreationtime = Convert.ToDateTime(dr["UserCreationtime"]); if (ds.Tables[i].Columns.Contains("Userhobby")) obj.Userhobby = Convert.ToString(dr["Userhobby"]); list.Add(obj); } } } return list; } catch (Exception) { throw; } }
namespace DAL { public class Family { //实例化一个DBbase对象 static DBbase db = new DBbase(); //查询用户数据 public static List<User> User() { //通过实体中的属性访问 拼接一个你需要的SQL语句 StringBuilder strSQL = new StringBuilder(); strSQL.Append("Select Theserialnumber, UserID, UserName, UserSet, Userphone, work.UserworkType,Details.WorkDetailsSituation,[level].UserleverlType,[type].UserType, UserCreationtime, hobby.Userhobby from [User] "); strSQL.Append("inner join Work on Work.UserworkID=[User].UserworkID "); strSQL.Append("inner join [level] on [level].UserlevelID=[user].UserlevelID "); strSQL.Append("inner join UserType as [type] on [type].UserTypeID=[USER].UserTypeID "); strSQL.Append("inner join WorkDetails as Details on Details.WorkDetailsID=Work.WorkDetailsID "); strSQL.Append("inner join Userhobby as hobby on hobby.UserhobbyID=[user].UserhobbyID"); return db.GetDataSet(strSQL.ToString()); } } }
步骤八:创建一个Web窗体aspx文件 设为启动项,在窗体前端编写HTML你需要展示的东西
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="HomePage.aspx.cs" Inherits="WebForms.HomePage" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style type="text/css"> table { margin: auto; border-style: dotted; border-color: #adabab; text-align: center; width: 80%; } </style> <title>WebForms</title> </head> <body> <form id="form1" runat="server"> <div> <p>直接绑定</p> <ul> <li><%=UserID %></li> <li><%=UserName %></li> </ul> <p>循环遍历对象</p> <table> <tr> <td>用户编号</td> <td>用户姓名</td> <td>用户性别</td> <td>用户电话</td> <td>工作类型</td> <td>工作详情</td> <td>用户权限</td> <td>用户类型</td> <td>入户时间</td> <td>用户性趣</td> </tr> <%for (int i = 0; i < User.Count; i++) { foreach (var item in User) {%> <tr> <td><%=item.UserID%></td> <td><%=item.UserName%></td> <td><%=item.UserSet%></td> <td><%=item.Userphone%></td> <td><%=item.UserworkType%></td> <td><%=item.WorkDetailsSituation%></td> <td><%=item.UserleverlType%></td> <td><%=item.UserType%></td> <td><%=item.UserCreationtime%></td> <td><%=item.Userhobby%></td> </tr> <% } } %> </table> </div> </form> </body> </html>
using Model; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebForms { public partial class HomePage : System.Web.UI.Page { public List<User> User=new List<Model.User>(); public string UserID = ""; public string UserName = ""; /// <summary> /// 首页加载 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) { User = DAL.Family.User(); UserID = User[0].UserID; UserName = User[0].UserName; } } }
步骤九:展示页面效果
总体来说:万变不离其中。
总结一下:其实挺简单的。
以上是关于一个简单的ASP.NET Web Forms应用程序的主要内容,如果未能解决你的问题,请参考以下文章
ASP.NET Web Forms – Button 控件简介
Telerik UI for ASP NET AJAX:在一半时间内为任何浏览器和设备构建Web Forms应用程序
ASP.NET Web Forms – ArrayList 对象简介
Asp .Net web Forms 文件上传(无flash控制)