作業(10)
阿新 • • 發佈:2017-10-11
進入 管理平臺 rom generic sha def div itl catch
3、功能實現(30分)
完成密碼修改功能,具體要求如下:
(1)用戶進入“訓戰合一教育管理平臺”的密碼修改頁面,列出包含用戶名、舊密碼、新密碼和確認密碼的表單,顯示效果如圖1所示。
(2)在如圖1所示的網頁中實現修改指定用戶的密碼。(註:應提供可操作的交互性)。
圖1 “訓戰合一教育管理平臺”密碼修改頁面原型
VS裏的效果圖:
各家瀏覽器的效果圖(待改進。。):
Default.aspx代碼如下:
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 2 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <title></title> 8 </head> 9 <body> 10<form id="form1" runat="server"> 11 修改密碼<br/> 12 13 <asp:Label ID="Label1" runat="server" Text="用 戶 名:"></asp:Label> 14 <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox> 15 <br /> 16 <asp:Label ID="Label2" runat="server" Text="舊 密 碼:"></asp:Label> 17 <asp:TextBox ID="txtPassword" runat="server" 18 ontextchanged="TextBox2_TextChanged"></asp:TextBox> 19 <br /> 20 <asp:Label ID="NewPassword" runat="server" Text="新 密 碼:"></asp:Label> 21 <asp:TextBox ID="txtNewPassword" runat="server"></asp:TextBox> 22 <br /> 23 <asp:Label ID="RegPassword" runat="server" Text="確認密碼:"></asp:Label> 24 <asp:TextBox ID="txtRegPassword" runat="server"></asp:TextBox> 25 <br /> 26 <br /> 27 28 <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="提交"/> 29 30 <asp:Button ID="Button2" runat="server" Text="清空" /> 31 <br /> 32 </form> 33 </body> 34 </html>
Default.aspx.cs代碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Configuration; using System.Data.SqlClient; using System.Data; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void TextBox2_TextChanged(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { //1.定義連接字符串 string connstr = ConfigurationManager.ConnectionStrings["TrainSystemConnectionString"].ConnectionString; //2.寫SQL語句 using ( SqlConnection conn=new SqlConnection(connstr)) { conn.Open(); //3.查詢語句 string selectstr = string.Format("select count (*) from Sys_User where Password=‘{0}‘ and UserName=‘{1}‘", txtPassword.Text.Trim(),txtUserName.Text.Trim()); //4.創建對象 SqlCommand cmd=new SqlCommand(selectstr,conn); //5.執行 try { int i = (int)cmd.ExecuteScalar(); if (i == 1) { string updatestr = string.Format("update Sys_User set Password=‘{0}‘ where UserName=‘{1}‘", txtNewPassword.Text.Trim(),txtUserName.Text.Trim()); //cmd=new SqlCommand (updatestr,conn); cmd.CommandText = updatestr; //cmd.Connection = conn; try { cmd.ExecuteNonQuery(); Response.Write("修改成功"); } catch { Response.Write("修改不成功"); } } else Response.Write("用戶名或密碼錯誤,請重新輸入!"); } catch { Response.Write("查詢不成功"); } } } }
web.config代碼如下:
1 <?xml version="1.0"?> 2 <!-- 3 有關如何配置 ASP.NET 應用程序的詳細信息,請訪問 4 http://go.microsoft.com/fwlink/?LinkId=169433 5 --> 6 7 <configuration> 8 9 <connectionStrings> 10 <add name="TrainSystemConnectionString" connectionString="Data Source=T134;Initial Catalog=TrainSystem;Integrated Security=True" 11 providerName="System.Data.SqlClient" /> 12 </connectionStrings> 13 <system.web> 14 <compilation debug="false" targetFramework="4.0" /> 15 </system.web> 16 17 </configuration>
總結一下經常用到的“套路”:
1.創建連接字符串 string connstr
2.創建連接對象 SqlConnection conn
3.創建查詢字符串 string selectstr
4.創建命令對象 SqlCommand cmd
5 執行命令
未完成部分:
新密碼和確認密碼不一致無錯誤提示。
作業(10)