My first ASP.NET Project summary
Recently, I was busy at the end of the term and didn't have time to update my blog. Here's one I'll do for myself ASP.NET Project (equipment management system) to summarize, the page template is the master, which greatly reduces the amount of code. Finally, the source code and database of the project are attached at the bottom of the article.
-
Let's see the effect first
1.1 landing page
1.2 welcome page
1.3 home page / list page
1.4 query effect (full field query)
1.5 details page / new, modify -
Login sample demo
In order to let you see more clearly, I wrote the operation of connecting to the database directly in the login example. And considering the data security, I hash the password.
/// <summary> ///Log in /// </summary> /// <param name="sender"></param> /// <param name="e"></param> [Obsolete] protected void Button1_Click(object sender, EventArgs e) { //[1] Judge whether the account password is empty if (TextBox1.Text.Trim() =="" || TextBox2.Text.Trim() == "") { //Prompt account password cannot be empty //Response.Write ("< script > alert ('account password cannot be empty ') < / script >"); Label1.Text = "account number/Password cannot be empty"; } else { //Clear prompt Label1.Text = ""; //Create database connection SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=123;database=sbgl_db"); //Open database connection con.Open(); //Query user password through user name string strsql = "select Password from Userinfo where UserName='" + TextBox1.Text.Trim()+"'"; //SqlDataAdapter myda = new SqlDataAdapter(strsql,con); SqlDataReader dr = new SqlCommand(strsql,con).ExecuteReader(); if (dr.Read()) { //SHA1 encryption of password string TBpwd = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text, "SHA1"); if (TBpwd == dr["Password"].ToString()) { //Create cookie s FormsAuthentication.SetAuthCookie(TextBox1.Text, false); Session["username"] = TextBox1.Text.Trim(); //Log in successfully and jump to the page Response.Redirect("welcome.aspx"); } else { //Password error Label1.Text = "Password error"; } } else { //user does not exist Label1.Text = "user does not exist"; } //Close database connection con.Close(); } }
- Function example
The following is a complete process of table parsing.
3.1 first of all, we can see in the previous effect of [home page / list page] that when we select the navigation of the left sidebar, the content area on the right will switch to display the corresponding data list. In fact, this is very simple. A gridview control can be implemented by binding to a data source. The following is the core logic code:
/// <summary> ///Encapsulates binding data operations /// </summary> public void BindDates() { //Instantiation class publicDB pb = new publicDB(); //Query sql statement string strsql = "select * from Assetinfo"; //Call the BindDate function object set = pb.BindDate(strsql); //Bind data source GridView1.DataSource = set; GridView1.DataBind(); }
3.11 core code of (full field) query
/// <summary> ///Query operation /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void butQuery(object sender, EventArgs e) { string param = TextBox1.Text; //string queryStr = "select * from Assetinfo where AssetDesc like '%"+param+"%'"; string queryStr = "SELECT * FROM Assetinfo WHERE CONCAT(ISNULL(AssetNo,''),ISNULL(AssetDesc,''),ISNULL(AssetType,''),ISNULL(CheckinDate,''),ISNULL(Other,'')) LIKE '%" + param + "%'"; DataSet myset = publicDB.ExcuteDataSet(queryStr); GridView1.DataSource = myset; GridView1.DataBind(); }
3.12 the complete code is as follows (including query, page turning, deletion, and operation of jump detail page, etc.)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; using System.Configuration; public partial class home : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { BindDates(); GridView1.DataKeyNames = new string[] { "AssetNo" }; } /// <summary> ///Query operation /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void butQuery(object sender, EventArgs e) { string param = TextBox1.Text; //string queryStr = "select * from Assetinfo where AssetDesc like '%"+param+"%'"; string queryStr = "SELECT * FROM Assetinfo WHERE CONCAT(ISNULL(AssetNo,''),ISNULL(AssetDesc,''),ISNULL(AssetType,''),ISNULL(CheckinDate,''),ISNULL(Other,'')) LIKE '%" + param + "%'"; DataSet myset = publicDB.ExcuteDataSet(queryStr); GridView1.DataSource = myset; GridView1.DataBind(); } /// <summary> ///Page turning operation /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_PageIndexChanging1(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; GridView1.DataBind(); } /// <summary> ///Delete operation /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { //sql statement string string delete_sql = "delete from Assetinfo where AssetNo='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'"; bool delete = ExceSQLs(delete_sql); if (delete) { //Redirect refresh page Response.Write("<script>alert('Delete successfully!');window.location.href = window.location.href </script>"); } else { Response.Write("<script>alert('Deletion failed!')</script>"); } } /// <summary> ///Encapsulates binding data operations /// </summary> public void BindDates() { //Instantiation class publicDB pb = new publicDB(); //Query sql statement string strsql = "select * from Assetinfo"; //Call the BindDate function object set = pb.BindDate(strsql); //Bind data source GridView1.DataSource = set; GridView1.DataBind(); } /// <summary> ///It encapsulates the method of passing sql statement to call excelsql to execute sql statement /// </summary> ///< param name = "sqlstr" > sql statement to execute < / param > ///< returns > returns the status of sql statement execution, that is, whether it is successful or not < / returns > public bool ExceSQLs(string sqlStr) { //Instantiation class publicDB pb = new publicDB(); //Call excel sql to execute sql statement and return bool value (that is, whether the sql execution is successful or not) bool sqtState = pb.ExceSQL(sqlStr); return sqtState; } /// <summary> ///Modify operation /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { string id = GridView1.DataKeys[e.NewEditIndex]["AssetNo"].ToString(); Response.Redirect("equipmentUpdate.aspx?AssetNo=" + id); } /// <summary> ///Click new to jump to edit details page /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void toAdd(object sender, EventArgs e) { Response.Redirect("equipmentAdd.aspx"); } /// <summary> ///Refresh operation /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void refresh(object sender, EventArgs e) { Response.Write("<script>window.location.href = window.location.href </script>"); } }
3.2 I just mentioned the jump details, and I will introduce the details page below.
Because there are many fields (28) in this project, it's not nice to add operations when repairing directly on a total page, so I specially added a detail page to complete the operation of adding / modifying data.
The main functions include saving, clearing (resetting), creating and returning
The code is as follows:
3.2.1 add operation
Tip: an external public class, publicDB, is used here. This class is written by myself and encapsulates the basic operations of data / database, such as connecting data, adding, deleting, modifying and querying.
/// <summary> ///Save / update operation /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void butSave(object sender, EventArgs e) { //insert sql statement string addSql = "insert into Assetinfo(AssetNo,AssetModel,EquipNumber,AssetDesc,AssetType,AssetStat,AssetBooker,StockDate,CheckinDate,YearsOfService,ProduceDate,YearOfUse,SoftwareNo,RejectDate,Unit,EUser,UserPhone,NetConfInfo,Remark,CPU,Mainboard,GraphicsCard,HDisk,NetCard,Memory,CDROM,FloppyDriver,Other) values(@AssetNo,@AssetModel,@EquipNumber,@AssetDesc,@AssetType,@AssetStat,@AssetBooker,@StockDate,@CheckinDate,@YearsOfService,@ProduceDate,@YearOfUse,@SoftwareNo,@RejectDate,@Unit,@EUser,@UserPhone,@NetConfInfo,@Remark,@CPU,@Mainboard,@GraphicsCard,@HDisk,@NetCard,@Memory,@CDROM,@FloppyDriver,@Other)"; //Load all fields into data SqlParameter[] prams = new SqlParameter[28]; prams[0] = new SqlParameter("AssetNo", TextBox1.Text.Trim()); prams[1] = new SqlParameter("AssetModel", TextBox2.Text.Trim()); prams[2] = new SqlParameter("EquipNumber", TextBox3.Text.Trim()); prams[3] = new SqlParameter("AssetDesc", TextBox4.Text.Trim()); prams[4] = new SqlParameter("AssetType", TextBox5.Text.Trim()); prams[5] = new SqlParameter("AssetStat", TextBox6.Text.Trim()); prams[6] = new SqlParameter("AssetBooker", TextBox7.Text.Trim()); prams[7] = new SqlParameter("StockDate", TextBox8.Text.Trim()); prams[8] = new SqlParameter("CheckinDate", TextBox9.Text.Trim()); prams[9] = new SqlParameter("YearsOfService", TextBox10.Text.Trim()); prams[10] = new SqlParameter("ProduceDate", TextBox11.Text.Trim()); prams[11] = new SqlParameter("YearOfUse", TextBox12.Text.Trim()); prams[12] = new SqlParameter("SoftwareNo", TextBox13.Text.Trim()); prams[13] = new SqlParameter("RejectDate", TextBox14.Text.Trim()); prams[14] = new SqlParameter("Unit", TextBox15.Text.Trim()); prams[15] = new SqlParameter("EUser", TextBox16.Text.Trim()); prams[16] = new SqlParameter("UserPhone", TextBox17.Text.Trim()); prams[17] = new SqlParameter("NetConfInfo", TextBox18.Text.Trim()); prams[18] = new SqlParameter("Remark", TextBox19.Text.Trim()); prams[19] = new SqlParameter("CPU", TextBox20.Text.Trim()); prams[20] = new SqlParameter("Mainboard", TextBox21.Text.Trim()); prams[21] = new SqlParameter("GraphicsCard", TextBox22.Text.Trim()); prams[22] = new SqlParameter("HDisk", TextBox23.Text.Trim()); prams[23] = new SqlParameter("NetCard", TextBox24.Text.Trim()); prams[24] = new SqlParameter("Memory", TextBox25.Text.Trim()); prams[25] = new SqlParameter("CDROM", TextBox26.Text.Trim()); prams[26] = new SqlParameter("FloppyDriver", TextBox27.Text.Trim()); prams[27] = new SqlParameter("Other", TextBox28.Text.Trim()); if (publicDB.ExcuteNonQuery(addSql, prams) > 0) { Response.Write("<script>alert('Successfully added');window.location.href = window.location.href</script>"); } else { Response.Write("<script>alert('Add failed')</script>"); } }
3.2.2 reset and return operation
It's all about redirecting pages
/// <summary> ///Clear page data operation /// </summary> protected void butClean(object sender, EventArgs e) { //Redirect clear page data Response.Write("<script>window.location.href = window.location.href</script>"); } /// <summary> ///Return to superior page /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void butBack(object sender, EventArgs e) { Response.Redirect("equipment.aspx"); }
3.2.3 modify / update operation
/// <summary> ///Save / update operation /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void butSave(object sender, EventArgs e) { //1 sql string updateSQL = "update Assetinfo set Assetinfo.AssetModel=@AssetModel,Assetinfo.EquipNumber=@EquipNumber,Assetinfo.AssetDesc=@AssetDesc,Assetinfo.AssetType=@AssetType,Assetinfo.AssetStat=@AssetStat,Assetinfo.AssetBooker=@AssetBooker,Assetinfo.StockDate=@StockDate,Assetinfo.CheckinDate=@CheckinDate,Assetinfo.YearsOfService=@YearsOfService,Assetinfo.ProduceDate=@ProduceDate,Assetinfo.YearOfUse=@YearOfUse,Assetinfo.SoftwareNo=@SoftwareNo,Assetinfo.RejectDate=@RejectDate,Assetinfo.Unit=@Unit,Assetinfo.EUser=@EUser,Assetinfo.UserPhone=@UserPhone,Assetinfo.NetConfInfo=@NetConfInfo,Assetinfo.Remark=@Remark,Assetinfo.CPU=@CPU,Assetinfo.Mainboard=@Mainboard,Assetinfo.GraphicsCard=@GraphicsCard,Assetinfo.HDisk=@HDisk,Assetinfo.NetCard=@NetCard,Assetinfo.Memory=@Memory,Assetinfo.CDROM=@CDROM,Assetinfo.FloppyDriver=@FloppyDriver,Assetinfo.Other=@Other where AssetNo='" + index + "'"; //2 get all data of the page SqlParameter[] prams = new SqlParameter[27]; //prams[0] = new SqlParameter("AssetNo", TextBox1.Text.Trim()); prams[0] = new SqlParameter("AssetModel", TextBox2.Text.Trim()); prams[1] = new SqlParameter("EquipNumber", TextBox3.Text.Trim()); prams[2] = new SqlParameter("AssetDesc", TextBox4.Text.Trim()); prams[3] = new SqlParameter("AssetType", TextBox5.Text.Trim()); prams[4] = new SqlParameter("AssetStat", TextBox6.Text.Trim()); prams[5] = new SqlParameter("AssetBooker", TextBox7.Text.Trim()); prams[6] = new SqlParameter("StockDate", TextBox8.Text.Trim()); prams[7] = new SqlParameter("CheckinDate", TextBox9.Text.Trim()); prams[8] = new SqlParameter("YearsOfService", TextBox10.Text.Trim()); prams[9] = new SqlParameter("ProduceDate", TextBox11.Text.Trim()); prams[10] = new SqlParameter("YearOfUse", TextBox12.Text.Trim()); prams[11] = new SqlParameter("SoftwareNo", TextBox13.Text.Trim()); prams[12] = new SqlParameter("RejectDate", TextBox14.Text.Trim()); prams[13] = new SqlParameter("Unit", TextBox15.Text.Trim()); prams[14] = new SqlParameter("EUser", TextBox16.Text.Trim()); prams[15] = new SqlParameter("UserPhone", TextBox17.Text.Trim()); prams[16] = new SqlParameter("NetConfInfo", TextBox18.Text.Trim()); prams[17] = new SqlParameter("Remark", TextBox19.Text.Trim()); prams[18] = new SqlParameter("CPU", TextBox20.Text.Trim()); prams[19] = new SqlParameter("Mainboard", TextBox21.Text.Trim()); prams[20] = new SqlParameter("GraphicsCard", TextBox22.Text.Trim()); prams[21] = new SqlParameter("HDisk", TextBox23.Text.Trim()); prams[22] = new SqlParameter("NetCard", TextBox24.Text.Trim()); prams[23] = new SqlParameter("Memory", TextBox25.Text.Trim()); prams[24] = new SqlParameter("CDROM", TextBox26.Text.Trim()); prams[25] = new SqlParameter("FloppyDriver", TextBox27.Text.Trim()); prams[26] = new SqlParameter("Other", TextBox28.Text.Trim()); //3 perform update operation if (publicDB.ExcuteNonQuery(updateSQL, prams) > 0) { Response.Write("<script>alert('Modification succeeded');window.location.href = 'equipment.aspx'</script>"); } else { Response.Write("<script>alert('Modification failed')</script>"); } }
- Source code and database link
Link: https://pan.baidu.com/s/1GP8kP0dus6-B4cXTtWVtlg
Extraction code: 9249
It's more convenient to open Baidu online mobile App after copying this content
Thank you very much