在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1、ASP.NET的好坏处 CodeBehind:“前aspx后cs”的模式, 2、 Literal: TextBox: Button:OnCLientClick 3、Button、LinkButton、ImageButton让多个控件共享一个处理函数: 4、 Panal: HyperLink:引用站内内部资源的时候很方便(自动转换路径) 5、 FileUpload: 漏洞:(只允许上传指定类型的文件) System.IO.File.Delete(Server.Mapath.("~/upload/")); 6、实例:注册界面 Register.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Register.aspx.cs" Inherits="登陆注册.Register" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <body> <form id="form1" runat="server"> <div> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td> <asp:Label ID="LabelName" runat="server" Text="姓名"></asp:Label> </td> <td> <asp:TextBox ID="txtUserName" runat="server" AutoPostBack="True" ontextchanged="txtUserName_TextChanged"></asp:TextBox> <asp:Label ID="errorMsg" runat="server" Text="Label" CssClass="error" Visible="False"></asp:Label> </td> </tr> <tr> <td> <asp:Label ID="Label2" runat="server" Text="密码"></asp:Label> </td> <td> <asp:TextBox ID="txtPassWord" runat="server"></asp:TextBox> 密码强度: <span id="spanPassWord" ></span> </td> </tr> <tr> <td> <asp:Label ID="Label3" runat="server" Text="重复输入密码"></asp:Label> </td> <td> <asp:TextBox ID="txtPassWord_Second" runat="server"></asp:TextBox> </td> </tr> <tr> <td> <asp:Label ID="Label4" runat="server" Text="邮箱"></asp:Label> </td> <td> <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox> </td> </tr> <tr> <td> <asp:Button ID="btnRegister" runat="server" Text="注册" onclick="btnRegister_Click" /> </td> </tr> <tr> <td> <asp:Label ID="LabelErrorMsg" runat="server" Visible="False"></asp:Label> </td> </tr> </table> </div> </form> </body> <head id="Head1" runat="server"> <style type="text/css"> .error{background:Red;} </style> <title>注册用户</title> <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $("#<%=txtPassWord.ClientID %>").blur(function () { var str = $(this).val(); if (str.length <= 6) { $("#spanPassWord").text("弱"); } else { //含有数字、字母 var check = /\w/; if (check.test(str)) { $("#spanPassWord").text("强"); } else { $("#spanPassWord").text("中"); } } }); $("#<%=form1.ClientID %>").submit(function () { var pas1 = $("#<%=txtPassWord.ClientID %>").val(); var pas2 = $("#<%=txtPassWord_Second.ClientID %>").val(); if (pas1 != pas2) { alert("两次输入密码不一致,请重新输入"); return; } var sEmail = $("#<%=txtEmail.ClientID %>").val(); var check = /.+@.+/; if (!check.test(sEmail)) { alert("邮箱格式不正确,请重新输入"); return; } }); });</script> </head> </html> Register.apsx.cs 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using 登陆注册.DAL.DataSet1TableAdapters; 8 using System.Text.RegularExpressions; 9 10 namespace 登陆注册 11 { 12 public partial class Register : System.Web.UI.Page 13 { 14 protected void Page_Load(object sender, EventArgs e) 15 { 16 17 } 18 19 protected void btnRegister_Click(object sender, EventArgs e) 20 { 21 //服务器版测试是否符合要求:邮箱饿坏密码 22 if (txtPassWord.Text != txtPassWord_Second.Text) 23 { 24 LabelErrorMsg.Text = "密码不一致!"; 25 LabelErrorMsg.Visible = true; 26 return; 27 } 28 Regex reEmail = new Regex(@".+@.+"); 29 if (!reEmail.IsMatch(txtEmail.Text)) 30 { 31 LabelErrorMsg.Text = "Email格式不正确!"; 32 LabelErrorMsg.Visible = true; 33 return; 34 } 35 LabelErrorMsg.Visible = false; 36 T_userInfoTableAdapter adapter = new T_userInfoTableAdapter(); 37 if (adapter.GetDataByUserName(txtUserName.Text).Count >= 1) 38 { 39 errorMsg.Visible = true; 40 errorMsg.Text = "用户名重复,请重新输入!"; 41 return; 42 } 43 long userId=Convert.ToInt64(adapter.InsertUser(txtUserName.Text, txtPassWord.Text, 44 txtEmail.Text)); 45 //读取Session登陆信息 46 Session["登陆ID"] = userId; 47 Session["登陆标记"] = true; 48 //设置可以传参数的公共跳转页面 49 //作用1:告诉用户注册成功,作用2:防止用户点击刷新重复提交数据 50 Response.Redirect("target.aspx?Msg=注册成功即将转向登陆界面&RedirectURL=login.aspx"); 51 } 52 53 protected void txtUserName_TextChanged(object sender, EventArgs e) 54 { 55 T_userInfoTableAdapter adapter = new T_userInfoTableAdapter(); 56 if (adapter.GetDataByUserName(txtUserName.Text).Count >= 1) { 57 errorMsg.Visible = true; 58 errorMsg.Text = "用户名重复,请重新输入!"; 59 } 60 else{ 61 errorMsg.Visible = false; 62 } 63 } 64 } 65 } 其中要用到的正则表达式: 插入数据记录:INSERT INTO [dbo].[T_userInfo] ([sUserName], [sPassWord], [Email]) output Inserted.Id VALUES (@sUserName, @sPassWord, @Email) (1、增加SQL语句, (2、增加一个output Insert.Id 在Insert语句当中 7、三种控件的区别:html控件、服务端控件。runat=server 的控件(最好) a1.Attributes("aaa")="建属性赋值"; 8、验证控件 (1、 RequiredFieldVal: Validator共性 ValidstionGroup 分组(表单可以在页面内单个组提交,而不关联其他组)相同于按钮的name (2、RangeValidator:范围验证 (3、只有RequiredFieldValidator可以对字段为空进行验证 (4、CompareValidator:比较验证(类型校验、与其他控件比较、) RegularExpression: VaildationExpression 正则表达式验证 CustomVaildate自定义控件验证 ServerValidate服务端校验代码 (5、ValidationSummary汇总错误信息
母版: //操作模板页的控件 this.Master.Mapath(); 虚拟路径转化为客户端访问的路径(不是服务端控件): this.ResolveClientUrl();
|
请发表评论