ASP.NET项目大概构造
最近,因为期末作业,于是写了一个ASP.NET项目,不是很难,用vs2017写的(vs还可以的,挺好用的,特别是前端的路径选择真的是好啊!!!),写了几天,感觉还行,于是分享一下。 vs2017安装使用: https://blog.csdn.net/qq_36556893/article/details/79430133 .NET框架使用的语言有很多,而我的ASP.NET项目用的是C#。实话说,我这是第一次用C#写,感觉和java差不多。那我现在就简单说一下我的ASP.NET项目如何写的: 我的页面构造(不好看,有点混乱,现在有点后悔-。-):
前端:
1.页面:和html有点不同: - <tr>
- <td style="text-align:center;" class="form-text">
- 用户名:
- </td>
- <td class="form-input">
- <asp:TextBox ID="tbUSerName" runat="server">
- </asp:TextBox>
- <asp:RequiredFieldValidator ID="rfvName" runat="server" ControlToValidate="tbUserName" ErrorMessage="用户名不能为空"></asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td style="text-align:center;" class="form-text">
- 密 码:
- </td>
- <td>
- <asp:TextBox ID="tbPassword" runat="server" TextMode="Password">
- </asp:TextBox>
- <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="tbPassword" ErrorMessage="密码不能为空"></asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td colspan="2" style="text-align:center;">
- <asp:Button ID="btLogin" runat="server" OnClick="btLogin_Click" Text="登录" />
- </td>
- </tr>
复制代码
显示效果如下:
2.css样式:和html差不多,没大有区别
引入css样式: - <link rel="stylesheet" href="../css/addcss.css" />
复制代码
后台:因为我用的是web窗体,所以会自动生成后台:
页面加载时想调用这个方法:Page_Load 点击事件按钮调用的方法:btLogin_Click(object sender,EventArgs e):可以自动生成
1.跳转: - Response.Redirect("FirstPage.aspx");
复制代码
2.前端弹窗:像js的alert(“”) - this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('登陆成功!')</script>");
- return;
复制代码
3.获取前端页面的值:
第一种方法: 前端id.属性 - string strName = tbUSerName.Text;
- string strPwd = tbPassword.Text.Trim();
复制代码
第二种方法: Request[id] - //下面的包括了单选框,复选框,下拉框,input等
- string id = Guid.NewGuid().ToString("N");//生成uuid:只能用ndbpx
- string name = Request["name"];
- string age = Request["age"];
- // string sex1 = sex.SelectedValue;
- string sex = Request["sex"];
- string phone = Request["phone"];
- string grade = Request["grade"];
- string hobbies = "";
- foreach(ListItem item in hobby.Items)
- {
- if (item.Selected)
- {
- hobbies += item.Value + ",";
- }
- }
- if (name == "" || age == "" || sex == "" || phone == "" || grade == "" || hobbies == "")
- {
- this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('信息填写不完整!')</script>");
- return;
- }
- string remarks = Request["remarks"] == null ? "" : Request["remarks"];
复制代码
4.生成UUID: - string id = Guid.NewGuid().ToString("N");//生成uuid:注意:只能用ndbpx,自行百度
复制代码
c#生成UUID的方法:https://blog.csdn.net/csj1314520/article/details/53033447
5.项页面输出内容,使页面显示后台传出的数据
前台: - <asp:Label ID="lblMsg" runat="server" Text="" Class="selectd"></asp:Label>
复制代码
后台:
6.页面之间通信:
a->b a页面后台代码发送: - Session["id"] = str[0];
- Session["name"] = str[1];
- Session["age"] = str[2];
- Session["sex"] = str[3];
- Session["phone"] = str[4];
- Session["grade"] = str[5];
- Session["hobby"] = str[6];
- Session["remarks"] = str[7];
- Response.Redirect("update.aspx");
复制代码
b页面后台代码接收: - id = (string)Session["id"];
- name.Text = (string)Session["name"];
- phone.Text = (string)Session["phone"];
- sex.Text = (string)Session["sex"];
- age.Text = (string)Session["age"];
- string grade1 = (string)Session["grade"];
- ListItem item1 = grade.Items.FindByValue(grade1);
- if (item1 != null)
- {
- item1.Selected = true;
- }
- char c = ',';
- string[] str = ((string)Session["hobby"]).Split(c);
- foreach (string val in str)
- {
- ListItem item = hobby.Items.FindByValue(val);
- if (item != null)
- {
- item.Selected = true;
- }
- }
- remarks.Text = (string)Session["remarks"];
复制代码
7.连接数据库:ADO.NET操作数据库
sqlserver数据库下载: https://visualstudio.microsoft.com/zh-hans/thank-you-downloading-visual-studio/?sku=community&rel=15 安装教程:https://blog.csdn.net/xyy410874116/article/details/26349465 修改代码里的MySqlCon,这一步用来连接到数据库,至关重要。
在“服务器资源管理”处选中数据库,然后可以在“属性”窗口找到“连接字符串”,复制其内容,赋给MySqlCon。比如我修改后是: Data Source=MS-20180307VTBY\SQLEXPRESS;Initial Catalog=member;Integrated Security=True
完成这些操作后,就可以在form里写代码来修改数据库了。
8.与数据库的操作: - using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Text;
- using System.Threading.Tasks;
- using System.Data.SqlClient;
- using System.Data;
- namespace WebApplication1.ado
- {
- public class Member
- {
- private string MySqlCon = "Data Source=MS-20180307VTBY\\SQLEXPRESS;Initial Catalog=member;Integrated Security=True"; //在vs中的数据库的详细信息中查找复制
- //其实是相当于提供一个可以传参的函数,到时候写一个sql语句,存在string里,传给这个函数,就会自动执行。
- public DataTable ExecuteQuery(string sqlStr) //用于查询;DataTable
- {
- SqlConnection con = new SqlConnection(@MySqlCon);
- con.Open();
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = con;
- cmd.CommandType = CommandType.Text;
- cmd.CommandText = sqlStr;
- DataTable dt = new DataTable();
- SqlDataAdapter msda;
- msda = new SqlDataAdapter(cmd);
- msda.Fill(dt);
- con.Close();
- return dt; //返回查询出的数据表
- }
- public Boolean ExecuteQuery1(string sqlStr) //用于查询;Boolean
- {
- SqlConnection con = new SqlConnection(@MySqlCon);
- con.Open();
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = con;
- cmd.CommandType = CommandType.Text;
- cmd.CommandText = sqlStr;
- SqlDataReader dr = cmd.ExecuteReader();
- Boolean b = false;
- if (dr.HasRows)
- {
- b = true;
- }
- else
- {
- b = false;
- }
- con.Close();
- return b; //返回boolean表示是否查询出数据
- }
- public string[] ExecuteQuery2(string sqlStr) //用于查询;string[]
- {
- SqlConnection con = new SqlConnection(@MySqlCon);
- con.Open();
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = con;
- cmd.CommandType = CommandType.Text;
- cmd.CommandText = sqlStr;
- SqlDataReader dr = cmd.ExecuteReader();
- string[] str = new string[8];
- if (dr.HasRows)
- {
- dr.Read();
- dr["sex"].ToString();
- for(int i = 0; i <= 7; i++)
- {
- str[i] = (dr.GetString(i)==null|| dr.GetString(i).Length==0)?"": dr.GetString(i);
- }
- }
- else
- {
-
- }
- con.Close();
- return str;
- }
- public int ExecuteUpdate(string sqlStr) //用于增删改;
- {
- SqlConnection con = new SqlConnection(@MySqlCon);
- con.Open();
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = con;
- cmd.CommandType = CommandType.Text;
- cmd.CommandText = sqlStr;
- int iud = 0;
- iud = cmd.ExecuteNonQuery();
- con.Close();
- return iud; //返回受影响的行数
- }
- }
- }
复制代码
如: 查询: - string str = "<table border='1' width='100%'><tr>";
- Member member = new Member();
- string sql = "select * from member";
- DataTable table = member.ExecuteQuery(sql);
- //输出表的列名
- for(int i = 0; i < table.Columns.Count; i++)
- {
- str = str + "<th colspan="2" style="text - align: center; ">" + table.Columns[i].ColumnName + "</th>";
- }
- str = str + "</tr>";
- //输出表的数据
- for(int i = 0; i < table.Rows.Count; i++)
- {
- DataRow row = table.Rows[i];
- str = str + "<tr>";
- for(int j = 0; j < table.Columns.Count; j++)
- {
- str = str + "<td colspan="2" style="text - align: center; ">" + row[j]+"</td>";
- }
- str = str + "</tr>";
- }
- str = str + "</table>";
- lblMsg.Text = str;
复制代码
详解在:https://blog.csdn.net/kiss__soul/article/details/80316788 *
问题解决:
可能会出现一些问题,如:WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping 其实就是缺少路径或者运行空间 详解:https://blog.csdn.net/ayang1986/article/details/45042909 好了,暂时就先这些吧,其实这asp.net和java的某些框架差不多,如果看不懂的话,我还有完整项目(包括数据库文件,是sqlServer数据库)在“我的资源”里,可以下载,直接能运行,里面注解很清晰。
项目下载:
https://download.csdn.net/download/weixin_43075298/10890259 来源:https://blog.csdn.net/weixin_43075298/article/details/85257443 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |