A simple ASP.NET Web Forms application

ASP.NET supports three different development model: Web Pages (Web page), MVC (Model View Controller Model - View - Controller), Web Forms (Web Forms)

I have to show you that Web Forms (Web Forms)

First: our need to understand, what is Web Forms.

          Web Forms is one of the three to create ASP.NET Web sites and Web applications programming mode.

          Web Forms is the oldest ASP.NET programming model is the integration of HTML, server controls and event driven web server code.

          Web Forms is then displayed as HTML pages generated on the server is compiled and executed by the server.

          There are hundreds of Web Forms Web controls and Web components to create a user with access to data-driven websites.

          For more details, please refer to: http://www.runoob.com/aspnet/aspnet-intro.html

Second: Web Forms to build

           Nonsense is not to say, or to directly manipulate it.

           Step one: Determine your development environment: vs2015 sql2008.

           Step two: all things are inseparable from development to build the database, build your database, insert a piece of data to see if there is data, which I do not nonsense.

-- 用户表
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

 

           Step three: Open your Visual Studio, the new project ---ASP.NET Web applications --- --- empty WebForms applications.

           Step Four: build a library Model (model) and a data access layer DAL

           Step Five: build a ADO.NET Entity Data Model Code First from the database in the Model

           Step Six: establish a reference (index)  

                          DAL frame assemblies and index System.Configuration Model System Profile

                          WebForms application index DAL and Model

            Step 7: class structures DBbase DAL data access layer for handling CRUD model data

        // read the configuration file, connect to the database statement 
        public  static  String strCon System.Configuration.ConfigurationManager.ConnectionStrings = [ " Family " ] .ConnectionString; 

        // instantiate a connection object CON   
        the SqlConnection CON = new new the SqlConnection (strCon); 

        // test connection is open   
        public  void Connection () 
        { 
            iF ( the this .con.State == ConnectionState.Closed) 
            { 
                the this .con.Open (); 
            } 
        } 
        // the strSQL statement provided by the database access, a set of return List 
        public List <the 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 the DAL 
{ 
    public  class Family 
    { 
        // instantiate objects DBbase a 
        static DBbase DB = new new DBbase (); 

        // querying user data 
        public  static List <the User> the User () 
        { 
            // by splicing entity attribute access you need a SQL statement 
            the StringBuilder strSQL = new new the StringBuilder (); 
            strSQL.Append ( " the Select Theserialnumber, the UserID, UserName, UserSet, USERPHONE, work.UserworkType, Details.WorkDetailsSituation, [Level] .UserleverlType, [type] .UserType, UserCreationtime, Hobby from .Userhobby [the 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());
        }
    }
}

 

            Step Eight: Create a Web Form aspx file as the startup item, you need to write HTML form displayed in front of the thing

 

<%@ 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>
            <P> loops through Object </ p>
                <li><%=UserName %></li>
            </ UL>

            <the Table> 
                <TR> 
                    <td> User ID </ td> 
                    <td> User Name </ td> 
                    <td> Gender </ td> 
                    <td> User phone </ td> 
                    <td> Work Type </ td > 
                    <td> work Detail </ td> 
                    <td> user permissions </ td> 
                    <td> user type </ td> 
                    <td> home time </ td> 
                    <td> user exotic </ 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;
        }
       
    }
}

           Step 9: showing results page

Overall: always changing.

  To summarize: In fact, quite simple.

Guess you like

Origin www.cnblogs.com/jstblog/p/12289742.html