上周用C#写的.NET framework Web项目

news/2024/6/18 21:55:28 标签: c#, .net

数据库搭建:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述在这里插入图片描述

在这里插入图片描述
资源管理器的内容

Functions.cs文件负责连接数据库,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
namespace EHotelMS2.Models
{ 
    public class Functions
{
    private SqlConnection Con;
    private SqlCommand Cmd;
    private DataTable dt;
    private string ConStr;
    private SqlDataAdapter sda;

    public int setData(string Query)
        {
        int Cnt;
    if(Con.State == ConnectionState.Closed)
        {
        Con.Open();
        }
Cmd.CommandText = Query;
Cnt = Cmd.ExecuteNonQuery();
Con.Close();
return Cnt;
        }


        public DataTable GetData(string Query)
{
    dt = new DataTable();
    sda = new SqlDataAdapter(Query, ConStr);
    sda.Fill(dt);
    return dt;


}
public Functions()
{
    ConStr = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\DC2020\Documents\EHotelDb1.mdf;Integrated Security=True;Connect Timeout=30";
//这段内容右键数据库,点击属性,查看连接字符串,前面加上@
    Con = new SqlConnection(ConStr);
    Cmd = new SqlCommand();
    Cmd.Connection = Con;
}

    }

}

Login.aspx内容:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="EHotelMS2.View.Login" EnableEventValidation="false"%>

<!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 rel="stylesheet" href="../Assets/Libraries/Bootstrap/css/bootstrap.min.css" />
    <style>
        body{
            background-image:url(../Assets/Images/hotel4.jpg);
            background-size:cover;
        }
        .container-fluid{
            opacity:0.9;
        }
    </style>
</head>
<body>

    <form id="form1" runat="server">
        <div>
            <div class="container-fluid">
        <div class="row" style="height:200px"></div>
        <div class="row">
            <div class="col-md-4"></div>
            <div class="col-md-4 bg-light rounded-3">
                <h1 class="text-text-success text-center">皇家理工度假酒店</h1>
                <form>
  <div class="mb-3">
    <label for="UserTb" class="form-label">用户名</label>
    <input type="text" class="form-control" id="UserTb" runat="server" required="required">
   
  </div>
  <div class="mb-3">
    <label for="PasswordTb" class="form-label">密码</label>
    <input type="password" class="form-control" id="PasswordTb" runat="server" required="required">
  </div>
  <div class="mb-3">
      <label id="ErrMsg" class="text-danger" runat="server"></label>
    <input type="radio" id="AdminCb" runat="server" name="Role"><label class="text-success">管理员</label>
    <input type="radio" id="UserCb" runat="server" name="Role"><label class="text-success">用户</label>
  </div>
                    <div class="d-grid">       
                        <asp:Button ID="LoginBtn" runat="server" Text="登陆" class="btn btn-success btn-block" OnClick="LoginBtn_Click" />
                    </div>
  
                    <br />
</form>
            </div>
            <div class="col-md-4"></div>
        </div>
        
    </div>
        </div>
    </form>
</body>
</html>

Login.aspx.cs内容:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EHotelMS2.View
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Con = new Models.Functions();
        }
        Models.Functions Con;
        protected void LoginBtn_Click(object sender, EventArgs e)
        {
            // Response.Redirect("Admin/Rooms.aspx");
            if (AdminCb.Checked)
            {
                if (UserTb.Value == "Admin" && PasswordTb.Value == "123456")
                {
                    Session["UserName"] = "Admin";
                    //Session缓存
                    Response.Redirect("https://localhost:44386/View/Admin/Categories.aspx");
                }
                else
                {
                    ErrMsg.InnerText = "无效的管理员!!!";
                }
            }
            else
            {
                string Query = "select UId,UName,UPass from UserTbl where UName='{0}' and UPass='{1}'";
                Query = string.Format(Query, UserTb.Value, PasswordTb.Value);
                DataTable dt = Con.GetData(Query);
                if (dt.Rows.Count == 0)
                {
                    ErrMsg.InnerText = "无效的用户!!!";
                }
                else
                {
                    Session["UserName"] = dt.Rows[0][1].ToString();
                    Session["UId"] = dt.Rows[0][0].ToString();
                    Response.Redirect("https://localhost:44312/View/Users/Booking.aspx");
                }
            }
        }
    }
}

Categories.aspx的内容:

%@ Page Title="" Language="C#" MasterPageFile="~/View/Admin/AdminMaster.Master" AutoEventWireup="true" CodeBehind="Categories.aspx.cs" Inherits="EHotelMS2.View.Admin.Categories" EnableEventValidation="false"%>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>  
<asp:Content ID="Content2" ContentPlaceHolderID="MyBody" runat="server">
    <div class="container-fluid">
    <div class="row">
    <div class = "col-4"></div>
    <div class = "col-4"><h1 class="text-success text-center">房型管理</h1></div>
    <div class = "col-4"></div>
    <div class = "col-4"></div>
       </div>
    <div class ="row">
    <div class="col-md-3">

<form>
<div class= "mb-3">
<label for= "CatNameTb" class= "form-label"> 房型名称 </label>
<input type = "text" class= "form-control" id = "CatNameTb" runat="server">
</div>
         
   <div class= "mb-3">
 <label for= "RemarkTb" class= "form-label">标签</label>
  <input type = "text" class= "form-control" id = "RemarkTb" runat="server">
   
      </div>
   
<div class="row">
    <div class="col d-grid">
        <asp:Button ID="EditBtn" runat="server" Text="编辑" class="btn btn-warning btn-block" OnClick="EditBtn_Click"/>
    </div>
    <div class="col d-grid">
        <asp:Button ID="DeleteBtn" runat="server" Text="删除" class="btn btn-danger btn-block" OnClick="DeleteBtn_Click"/>
    </div>           
 </div>

     <br/>
<div class= "d-grid">
<label id ="ErrMsg" runat ="server" class="text-danger"></label>

    <asp:Button ID="SaveBtn" runat="server" Text ="保存" CssClass="btn btn-success btn-block " OnClick="SaveBtn_Click"/>
</div>
 <br/>
 </form>                              
</div>

    <div class = "col-md-9">

        <asp:GridView ID="CategoriesGV" runat="server" Class="table" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="CategoriesGV_SelectedIndexChanged" AutoGenerateSelectButton="True">
            <AlternatingRowStyle BackColor="White"/>
            <Columns>
                <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Select" Text="选择"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White"/>
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White"/>
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center"/>
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy"/>
            <SortedAscendingCellStyle BackColor="#FDF5AC"/>
            <SortedAscendingHeaderStyle BackColor="#4D0000"/>
            <SortedDescendingCellStyle BackColor="#FCF6C0"/>
            <SortedDescendingHeaderStyle BackColor="#820000"/>
        </asp:GridView>

    </div>

    </div>

    </div>
</asp:Content>

Categories.aspx.cs的内容:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EHotelMS2.Models;


namespace EHotelMS2.View.Admin
{
    public partial class Categories : System.Web.UI.Page
    {

        Models.Functions Con;



        protected void Page_Load(object sender, EventArgs e)
        {
            Con = new Models.Functions();
            ShowCategories();

        }
        private void ShowCategories()
        {
            string Query = "select CatId as Id,CatName as Categories,CatRemarks from CategoryTb1";
            CategoriesGV.DataSource = Con.GetData(Query);
            CategoriesGV.DataBind();
            CategoriesGV.HeaderRow.Cells[1].Text = "序号";
            CategoriesGV.HeaderRow.Cells[2].Text = "房型";
            CategoriesGV.HeaderRow.Cells[3].Text = "标签";
        }


        protected void SaveBtn_Click(object sender, EventArgs e)
        {
            try
            {

                string CatName = CatNameTb.Value;
                string Rem = RemarkTb.Value;
                string Query = "insert into CategoryTb1 values('{0}','{1}')";
                Query = string.Format(Query, CatName, Rem);
                Con.setData(Query);
                ErrMsg.InnerText = "房型已添加";

            }

            catch (Exception Ex) {

                ErrMsg.InnerText = Ex.ToString();
            }




        }

        int Key = 0;
        protected void CategoriesGV_SelectedIndexChanged(object sender, EventArgs e)
        {
            Key = Convert.ToInt32(CategoriesGV.SelectedRow.Cells[1].Text);
            CatNameTb.Value = CategoriesGV.SelectedRow.Cells[2].Text;
            RemarkTb.Value = CategoriesGV.SelectedRow.Cells[3].Text;

        }

        protected void EditBtn_Click(object sender, EventArgs e)//编辑
        {
            try
            {
                string CatName = CatNameTb.Value;
                string Rem = RemarkTb.Value;
                string Query = "update CategoryTb1 set CatName='{0}',CatRemarks='{1}' where CatId={2}";
                Query = string.Format(Query, CatName, Rem, CategoriesGV.SelectedRow.Cells[1].Text);
                Con.setData(Query);
                ShowCategories();
                ErrMsg.InnerText = "房型已更新!";

            }
            catch (Exception Ex)
            {
                ErrMsg.InnerText = Ex.Message;
            }
        }

        protected void DeleteBtn_Click(object sender, EventArgs e)
        {
            try
            {
                string CatName = CatNameTb.Value;
                string Rem = RemarkTb.Value;
                string Query = "delete from CategoryTb1 where CatId={0}";
                Query = string.Format(Query, CategoriesGV.SelectedRow.Cells[1].Text);
                Con.setData(Query);
                ShowCategories();
                ErrMsg.InnerText = "房型已删除!";

            }
            catch (Exception Ex)
            {
                ErrMsg.InnerText = Ex.Message;
            }
        }


    }
}

内容演示:
在这里插入图片描述
在这里插入图片描述

其他内容提示:
工具-选项,添加如下内容,防止控件没法添加
在这里插入图片描述
创建项目的时候,注意
在这里插入图片描述
这两个项目需要区分,选VB那个,aspx文件下不会有相关联的.cs文件

这个项目的层次大概是:母版页-包含母版页的web窗体-.cs文件


http://www.niftyadmin.cn/n/1311664.html

相关文章

课堂练习寻找水王

水王问题的实质就是如何快速在一串连续的数字里找到出现次数超过一半的数字。重点是最快&#xff0c;通过老师给出的算法思路&#xff0c;进行设计把这串数字放到一个数组里面&#xff0c;两个相邻的数字去做比较&#xff0c;如果相等&#xff0c;在向后比较&#xff0c;如果不…

jquery 录屏_新书出炉了,《asp.net4+jQuery 构建信息门户网站》,全程录屏,谢谢支持!...

经过半年时间的断断续续(期间还经历了我家宝宝的诞生)&#xff0c;此视频书籍终于出版了&#xff0c;此书籍针对的学习对象为初、中级学者&#xff0c;制作过程全程录屏。不敢称有多少含金量&#xff0c;但绝对是饱含用心在其中。在此感谢我的妻子对我长期以来的支持&#xff0…

golang代码片段

// 1.多goroutine间通信&#xff0c;channel使用 import ("fmt""math/rand""net""os""os/signal""time""crypto/sha256""crypto/sha1""crypto/md5""strings" )func Prod…

js long类型精度丢失_Long类型转json时前端js丢失精度解决方案

一、问题背景Java后端开发过程中&#xff0c;尤其是id字段&#xff0c;因数值太大&#xff0c;通过json形式传输到前端后&#xff0c;在js解析时&#xff0c;会丢失精度。二、解决思路将id字段序列化为json时&#xff0c;转换为字符串类型&#xff0c;前端传输到后端&#xff0…

Vue学习(五):列表渲染

<!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>列表渲染</title> </head> <body> <!--1.数组迭代--> <div id"example1"><ul><li v-for"item…

Array 对象方法总结

concat() var arr1 [1, 2, 3]; var arr2 [4, 5, 6]; var arr3 arr1.concat(arr2); console.log(arr3); // Output: [1, 2, 3, 4, 5, 6] //合并copyWithin() var arr [1, 2, 3, 4, 5]; arr.copyWithin(0, 3); console.log(arr); // Output: [4, 5, 3, 4, 5] //copywithin(ta…

python 常见异常类型

python标准异常 异常名称     描述 BaseException     所有异常的基类SystemExit             解释器请求退出KeyboardInterrupt          用户中断执行(通常是输入^C)Exception    …

jQuery选择函数

得到或设置文本内容或HTML内容或属性的值 $(“p”).addClass(css中定义的样式类型); 给某个元素添加样式 $(“img”).attr({src:”test.jpg”,alt:”test Image”}); 给某个元素添加属性/值&#xff0c;参数是map $(“img”).attr(“src”,”test.jpg”); 给某个元素添加属性/值…