一.说明
此文是小白在学习张晨光老师的视频教学<<Asp.Net WEB服务器编程技术>>中做的学习笔记,一些知识点也是跟着教程走的,大家也可以去老师的主页去学习,谢谢大家.
这一篇要练习的是,如下课程的代码:
二.增加母版页(Musci_Menu.Master)
右击项目/添加/新建项:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Musci_Menu.master.cs" Inherits="MyMusci.Musci_Menu" %>
<!DOCTYPE html>
<html>
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<link href="bootstrap-5.1.3-dist/css/bootstrap.css" rel="stylesheet" />
<script src="bootstrap-5.1.3-dist/js/bootstrap.bundle.js"></script>
<script src="js/jquery.js"></script>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
<style>
.img {
height:126px;
width:126px;
}
.bg-linear {
background:-webkit-linear-gradient(#ffffff,#63B8FF)
}
.Repeater {
width:200px;
padding:50px;
margin:10px;
display:inline-block;
background: linear-gradient(to bottom right ,#1E90FF,#FFC0CB, #FF69B4);
border-radius: 25px;
}
.recommend {
display:inline-flex;
width:400px;
}
</style>
<script>
btn_active = function (str) {
$("ul").find("a").removeClass("active");
$("#" + str).addClass("active");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="container-fluid">
<header class="d-flex justify-content-center py-3 bg-linear justify-content-lg-start">
<img alt="" src="img/music.png" style="width:50px;height:50px;margin-left:20px;margin-right:10px" /><span style="font-size:30px;color:#0a58ca;margin-right:150px;">薄雾音乐</span>
<ul class="nav nav-pills">
<li class="nav-item"><a href="main.aspx" class="nav-link" id="main" >发现音乐</a></li>
<li class="nav-item"><a href="#" class="nav-link ">我的主页</a></li>
<li class="nav-item"><a href="user_center.aspx" class="nav-link" id="center">个人中心</a></li>
<li class="nav-item"><a href="#" class="nav-link ">FAQs</a></li>
<li class="nav-item"><a href="#" class="nav-link ">About</a></li>
</ul>
</header>
</div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
<div style="width:100%;background-color:#363636;color:white;text-align:center;position:fixed;bottom:0;">
Copyright ©2021-2022 小白. 保留所有权利
</div>
</div>
</form>
</body>
</html>
三.优化个人中心(user_center.aspx)
右击项目/添加/新建项: 然后选择我们刚刚创建的母版页: 然后我们将Home.aspx里的代码转移至user_center.aspx里:前端
<%@ Page Title="" Language="C#" MasterPageFile="~/Musci_Menu.Master" AutoEventWireup="true" CodeBehind="User_Center.aspx.cs" Inherits="MyMusci.User_Center" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div class="container-fluid">
<table class="table">
<tbody>
<tr>
<td>账号</td>
<td><asp:Label ID="userId" runat="server"></asp:Label></td>
<td rowspan="3"><asp:Image ID="userPhoto" runat="server" CssClass="img" />
<br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="上传" OnClick="btnUpload_Click" />
</td>
</tr>
<tr>
<td>昵称</td>
<td><asp:TextBox ID="userNetname" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>性别</td>
<td><asp:DropDownList ID="userSex" runat="server">
<asp:ListItem Value="true">男</asp:ListItem>
<asp:ListItem Value="false">女</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>签名</td>
<td><asp:TextBox ID="userSign" runat="server" Height="101px" TextMode="MultiLine"></asp:TextBox></td>
</tr>
<tr>
<td>年龄</td>
<td><asp:TextBox ID="userAge" runat="server"></asp:TextBox></td>
</tr>
</tbody>
</table>
<asp:Button ID="sub_Btn" runat="server" Text="编辑" class="btn btn-primary" OnClick="sub_Btn_Click" />
</div>
<script>
btn_active("center");
</script>
</asp:Content>
后端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using Model;
namespace MyMusci
{
public partial class User_Center : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["userName"] == null)
{
Response.Redirect("login_new.aspx");
}
else
{
if (!IsPostBack)
{
GetUser();
}
}
}
private void GetUser()
{
string nameid = Session["userName"].ToString();
string sql = "select * from user_all where user_Name=?userName";
Hashtable ht = new Hashtable();
ht.Add("userName", nameid);
DataTable dt = ToolMysqlDate.executTable(sql, ht);
userId.Text = nameid;
userNetname.Text = dt.Rows[0]["u_nick"].ToString();
userSex.Items.FindByValue(dt.Rows[0]["u_sex"].ToString() == "True" ? "true" : "false").Selected = true;
userSign.Text = dt.Rows[0]["u_sign"].ToString();
userAge.Text = dt.Rows[0]["u_age"].ToString();
userPhoto.ImageUrl = dt.Rows[0]["u_photo"].ToString();
}
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string imgName = FileUpload1.FileName;
imgName = DateTime.Now.ToString("yyyymmddhhmmss") + imgName;
FileUpload1.SaveAs(Server.MapPath("\\user_img\\") + imgName);
userPhoto.ImageUrl = "\\user_img\\" + imgName;
}
else
{
Response.Write("<script>alert('需要先选择图片,再上传')</script>");
}
}
protected void sub_Btn_Click(object sender, EventArgs e)
{
string sql = "update user_all set " +
"u_Nick=?u_Nick,u_sex=?u_sex,u_Sign=?u_Sign,u_age=?u_age,u_photo=?u_photo " +
"where user_name=?user_name";
DataUser us = new DataUser();
us.user_name = userId.Text;
us.u_Nick = userNetname.Text;
us.u_sex = userSex.SelectedValue == "true" ? 1 : 0;
us.u_Sign = userSign.Text;
us.u_age = Convert.ToInt32(userAge.Text);
us.u_photo = userPhoto.ImageUrl;
Hashtable ht = new Hashtable();
ht.Add("user_name", us.user_name);
ht.Add("u_Nick", us.u_Nick);
ht.Add("u_sex", us.u_sex);
ht.Add("u_Sign", us.u_Sign);
ht.Add("u_age", us.u_age);
ht.Add("u_photo", us.u_photo);
int result = ToolMysqlDate.executeSql(sql, ht);
if (result > 0)
{
Response.Write("<script>alert('信息修改成功')</script>");
}
else
{
Response.Write("<script>alert('信息修改失败')</script>");
}
}
}
}
这样我们在登录后,会得到如下的个人中心界面(ps:登录界面的路径需要大家修改一下):
四.发现音乐界面(Main.aspx)
我们同样使用包含母版页的web窗体来创建发现音乐,创建完毕后,填入如下代码:
<%@ Page Title="" Language="C#" MasterPageFile="~/Musci_Menu.Master" AutoEventWireup="true" CodeBehind="Main.aspx.cs" Inherits="MyMusci.Main" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div class="container">
<div style="background-color:gainsboro">
<span class="text-center" style="font-size:30px;">歌曲推荐</span><br />
<div class="tab-content row">
<div id="home" class="container tab-pane active" style="align-content:center;"><br>
<%--这里放置asp:Repeater 控件,读取数据库数据--%>
<asp:Repeater ID="Repeater5" runat="server">
<ItemTemplate>
<%--这里采用特殊样式--%>
<div class="col recommend">
<img src="<%# Eval("m_photo") %>"" style="width:125px;height:125px" />
<a href="Musci_Main.aspx?id">
<p><%# Eval("ROWS") %><%# Eval("m_name") %></p><p><%# Eval("m_singer") %></p>
</a>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
<div id="menu1" class="container tab-pane fade"><br>
<asp:Repeater ID="Repeater6" runat="server">
<ItemTemplate>
<div class="col recommend">
<img src="img/music.png" style="width:125px;height:125px" />
<a href="Musci_Main.aspx?id">
<p><%# Eval("ROWS") %><%# Eval("m_name") %></p><p><%# Eval("m_singer") %></p>
</a>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
<div id="menu2" class="container tab-pane fade"><br>
<asp:Repeater ID="Repeater7" runat="server">
<ItemTemplate>
<div class="col recommend">
<img src="img/music.png" style="width:125px;height:125px" />
<a href="Musci_Main.aspx?id">
<p><%# Eval("ROWS") %><%# Eval("m_name") %></p><p><%# Eval("m_singer") %></p>
</a>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
</div>
<%--这里是歌曲推荐下方的按钮,点击可以切换下一页推荐歌曲--%>
<div class="d-flex justify-content-center">
<ul class="nav nav-pills" role="tablist">
<li class="nav-item">
<a class="nav-link active" data-bs-toggle="pill" href="#home">.</a>
</li>
<li class="nav-item">
<a class="nav-link" data-bs-toggle="pill" href="#menu1">.</a>
</li>
<li class="nav-item">
<a class="nav-link" data-bs-toggle="pill" href="#menu2">.</a>
</li>
</ul>
</div>
</div>
<div class="row">
<div class="col Repeater">
<span style="font-size:30px;align-content:center">热歌榜</span><p></p>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div class="row">
<a href="Musci_Main.aspx?id">
<p><%# Eval("ROWS") %> <%# Eval("m_name") %></p>
<p><%# Eval("m_singer") %></p>
</a>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
<div class="col Repeater">
<span style="font-size:30px;align-content:center">治愈榜</span><p></p>
<asp:Repeater ID="Repeater2" runat="server">
<ItemTemplate>
<div class="row">
<a href="Musci_Main.aspx?id">
<p><%# Eval("ROWS") %> <%# Eval("m_name") %></p>
<p><%# Eval("m_singer") %></p>
</a>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
<div class="col Repeater">
<span style="font-size:30px;align-content:center">国风榜</span><p></p>
<asp:Repeater ID="Repeater3" runat="server">
<ItemTemplate>
<div class="row">
<a href="Musci_Main.aspx?id">
<p><%# Eval("ROWS") %> <%# Eval("m_name") %></p>
<p><%# Eval("m_singer") %></p>
</a>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
<div class="col Repeater">
<span style="font-size:30px;align-content:center">欧美榜</span><p></p>
<asp:Repeater ID="Repeater4" runat="server">
<ItemTemplate>
<div class="row">
<a href="Musci_Main.aspx?id">
<p><%# Eval("ROWS") %> <%# Eval("m_name") %></p>
<p><%# Eval("m_singer") %></p>
</a>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
</div>
</div>
<script>
btn_active("main");
</script>
</asp:Content>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MyMusci
{
public partial class Main : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
Bindlist();
}
}
private void Bindlist()
{
string sql = "SELECT @rownum:=@rownum+1 AS ROWS,t.* FROM music t,"+
"(SELECT @rownum:=0)t1 ORDER BY m_clicknum DESC LIMIT 5 ";
Repeater1.DataSource = ToolMysqlDate.executTable(sql);
Repeater1.DataBind();
Repeater2.DataSource = ToolMysqlDate.executTable("SELECT @rownum:=@rownum+1 AS ROWS,t.* FROM music t ,(SELECT @rownum:=0)t1 WHERE m_type IN (SELECT m_number FROM music_type WHERE m_type='治愈') ORDER BY m_clicknum DESC LIMIT 5");
Repeater2.DataBind();
Repeater3.DataSource = ToolMysqlDate.executTable("SELECT @rownum:=@rownum+1 AS ROWS,t.* FROM music t ,(SELECT @rownum:=0)t1 WHERE m_type IN (SELECT m_number FROM music_type WHERE m_type='国风') ORDER BY m_clicknum DESC LIMIT 5");
Repeater3.DataBind();
Repeater4.DataSource = ToolMysqlDate.executTable("SELECT @rownum:=@rownum+1 AS ROWS,t.* FROM music t ,(SELECT @rownum:=0)t1 WHERE m_type IN (SELECT m_number FROM music_type WHERE m_type='欧美') ORDER BY m_clicknum DESC LIMIT 5");
Repeater4.DataBind();
Repeater5.DataSource = ToolMysqlDate.executTable("SELECT t.* FROM (SELECT @rownum:=@rownum+1 AS ROWS,t.* FROM music t ,(SELECT @rownum:= 0)t1 ORDER BY m_clicknum DESC)t WHERE ROWS BETWEEN(1 - 1)*9 AND(2 - 1) * 9 ORDER BY ROWS ");
Repeater5.DataBind();
Repeater6.DataSource = ToolMysqlDate.executTable("SELECT t.* FROM (SELECT @rownum:=@rownum+1 AS ROWS,t.* FROM music t ,(SELECT @rownum:= 0)t1 ORDER BY m_clicknum DESC)t WHERE ROWS BETWEEN(2 - 1)*9+1 AND(3 - 1) * 9 ORDER BY ROWS ");
Repeater6.DataBind();
Repeater7.DataSource = ToolMysqlDate.executTable("SELECT t.* FROM (SELECT @rownum:=@rownum+1 AS ROWS,t.* FROM music t ,(SELECT @rownum:= 0)t1 ORDER BY m_clicknum DESC)t WHERE ROWS BETWEEN(3 - 1)*9+1 AND(4 - 1) * 9 ORDER BY ROWS ");
Repeater7.DataBind();
}
}
}
歌曲推荐部分: 榜单部分:
五.ToolMysqlDate.cs增加方法
我们在之前的ToolMysqlDate.cs中添加如下方法:
public static DataTable executTable(String sql)
{
MySqlDataAdapter mda = new MySqlDataAdapter(sql, conn);
DataTable dt = new DataTable();
mda.Fill(dt);
return dt;
}
六.说明
推荐歌曲的图片存储位置(需要大家手动导入) 对应数据库的内容: 这样,一个比较简陋的音乐主页模块就做好了,谢谢大家
|