页面前面:
<div id="login" runat="server"> <span class="log_title">账号(昵称):</span><input class="log_input" runat="server" id="t_LogName" name="t_LogName" type="text" placeholder="请输入账号(昵称)" /> <span class="log_title">密码:</span><input class="log_input" runat="server" id="t_LogPass" name="t_LogPass" type="password" placeholder="请输入密码" /> <asp:Button ID="Button1" runat="server" Text="登陆" OnClick="Button1_Click" CssClass="button" EnableTheming="False" /> <asp:Button ID="Button2" runat="server" CssClass="button" Text="注册" PostBackUrl="~/regist.aspx"></asp:Button> </div> <div id="logstate" runat="server" visible="false">欢迎您:<span><asp:Literal ID="uesrName" runat="server"></asp:Literal></span><a runat="server" onserverclick="out_click">退出</a></div>
要引入的样式:
#login { top:0; left: 0; 580px;height:25px;background:pink;position: relative;float:left} .button { height: 25px;font-size: 14px; background-color: #0998ff; color: #FFF;padding:3px 12px 5px 12px;border: 0px; vertical-align: middle;margin-left:5px;float:left; } .button:hover{ background-color: #6699FF;color:#000;cursor:pointer } .log_input{background-color:#FFF; border:1px solid #d5cfc2;130px;height:23px; font-size:14px;vertical-align:middle;float:left} .log_title{90px; display:inline-block; text-align:right;line-height:25px; float:left;color:#FFF;} #Head_logstate {/*200px;*/height:25px;color:#FFF;font-size:14px;line-height: 25px;/*background:blue;*/float:left;position: relative;} #Head_logstate span { color:#f00;font-weight:800;margin-right:20px} #Head_logstate a {height:25px;font-size: 14px; background-color: #0998ff; color: #FFF;padding:0 12px 0 12px;border: 0px; vertical-align: middle;float:right} #Head_logstate a:hover{ background-color: #6699FF;color:#000;cursor:pointer }
说明一下,应该是我把这个放在控件Head里,所以引入页面的时候,
logstate这个样式找不到,后来,我就改是
#Head_logstate,才显示正常,而在控件里div的样式还是用的
<div id="logstate" runat="server" visible="false">....
控件的cs核心代码:
protected string mynames; protected string myID; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { HttpCookie cookie = Request.Cookies["nickname"]; if (cookie != null) { mynames = cookie.Value; GetLogin(); } } }
protected void Button1_Click(object sender, EventArgs e) { string name = t_LogName.Value.Trim(); string pw = t_LogPass.Value.Trim(); if (name.IndexOf("'") > 0 || name.IndexOf("--") > 0) { HelperJS.Show(this.Page, "账号(昵称)存在非法字符!"); return; } if (name != string.Empty && pw != string.Empty) { //检查用户名密码 DataTable dt = HelperExecuteSql.Query("select ID,nickname,pw from _user where nickname='" + name + "' and pw ='" + pw + "'").Tables[0]; if (dt.Rows.Count > 0) { myID = dt.Rows[0]["ID"].ToString(); //Session["nickname"] = name; //Session["ID"] = Convert.ToInt32(dt.Rows[0][0].ToString()); //uesrName.Text = dt.Rows[0]["nickname"].ToString(); if (Request["ReturnUrl"] == null || Request["ReturnUrl"] == "") { string nowtime = DateTime.Now.ToString(); //获取当前时间 HttpCookie cookie = new HttpCookie("nickname", HttpUtility.UrlEncode(name));//获取用户的用户名 cookie.Expires = DateTime.Now.AddHours(2);//设置cookie过期时间为2小时后 Response.Cookies.Add(cookie);//将cookie写入客户端 HttpCookie cookie1 = new HttpCookie("nowTime", nowtime); cookie1.Expires = DateTime.Now.AddHours(2);//设置cookie过期时间为2小时后 Response.Cookies.Add(cookie1);//将cookie1写入客户端 HttpCookie cookie2 = new HttpCookie("ID", myID);//获取用户的ID Response.Cookies.Add(cookie2);//将cookie2写入客户端 Response.Write(" <script language=javascript>window.location.href=document.URL;</script>"); //刷新 } else { Server.Transfer(Request["ReturnUrl"]); } } else { HelperJS.Show(this.Page, "用户名或密码不正确!"); } } else { HelperJS.Show(this.Page, "用户名或密码不能为空!"); } } protected void out_click(object sender, EventArgs e) //退出 { Response.Cookies["nickname"].Expires = DateTime.Now;//cookie将马上过期 login.Visible = true; logstate.Visible = false; Response.Write(" <script language=javascript>window.location.href=document.URL;</script>"); //刷新 }
参考资料 http://www.cnblogs.com/sosoft/p/3547471.html
编写Cookie
//方式1: Response.Cookies["username"].value="mike"; Response.Cookies["username"].Expires=DateTime.MaxValue; //方式2: HttpCookie acookie = new HttpCookie("last"); acookie.Value="a"; acookie..Expires=DateTime.MaxValue; Response.Cookies.Add(acookie); //多值Cookie的写法 http://www.cnblogs.com/sosoft/ //方式1: Response.Cookies["userinfo1"]["name"].value="mike"; Response.Cookies["userinfo1"]["last"].value="a"; Response.Cookies["userinfo1"].Expires=DateTime.MaxValue; //方式2: HttpCookie cookie = new HttpCookie("userinfo1"); cookie.Values["name"]="mike"; cookie.Values["last"]="a"; cookie.Expires=DateTime.MaxValue; //cookie.Expires = System.DateTime.Now.AddDays(1);//设置过期时间 1天 Response.Cookies.Add(cookie);
读取Cookie
If (Request.Cookies["userName"]!=null) { string str = Request.Cookies("userName").Value; } //多值Cookie的读取 If ( Request.Cookies["userInfo1"]!=null ) { string name=Request.Cookies["userInfo1"]["name"]; string last=Request.Cookies["userInfo1"]["last"]; } //读取 Cookie 集合 for(int i = 0 ;i<Request.Cookies.Count ;i++) { HttpCookie cookies = Request.Cookies; Response.Write("name="+cookies.Mame+"<br/>"); if (cookies.HasKeys )//是否有子键 { System.Collections.Specialized.NameValueCollection NameColl = aCookie.Values ; for(int j=0;j<NameColl.Count;j++) { Response.Write("子键名="+ NameColl.AllKey[j] +"<br/>"); Response.Write("子键值="+ NameColl[j] +"<br/>"); } } else { Response.Write("value="+cookies.Value+"<br/>"); } }
http://www.cnblogs.com/yinrq/p/5019448.html
ASP.NET对Cookie的CURD操作
//写入Cookie三种方式 //方式1 var cookie = new HttpCookie("name", "joye888"); Response.Cookies.Add(cookie); //方式2 Response.Cookies["name1"].Value = "joye8881"; Response.Cookies["name1"].Expires = DateTime.MaxValue; //方式3 var acookie = new HttpCookie("name2"); acookie.Value = "joye8882"; acookie.Expires = DateTime.MaxValue; Response.Cookies.Add(acookie); //写入多值Cookie //方式1: //Response.Cookies["userinfo"]["name"].Value = "joye888"; //Response.Cookies["userinfo"].Expires = DateTime.MaxValue; //方式2: HttpCookie cookie2 = new HttpCookie("userinfo"); cookie2.Values["name"] = "joye888"; cookie2.Expires = DateTime.Now.AddDays(1); Response.Cookies.Add(cookie2); //读取Cookie的值之前,应该确保该 Cookie 确实存在。否则,您将得到一个异常 //读取Cookie var httpCookie = Request.Cookies["name1"]; if (httpCookie != null) { var name = httpCookie.Value; } //多值Cookie的读取 var httpCookie1 = Request.Cookies["userInfo"]; if (httpCookie1 != null) { var name1 = httpCookie1["name"]; } //修改和删除cookie //修改的方法与创建方法相同 var cookieEdit = new HttpCookie("name", "joye888Edit"); cookieEdit.Expires = DateTime.Now.AddDays(-1); //将其有效期设置为过去的某个日期。当浏览器检查 Cookie 的有效期时,就会删除这个已过期的 Cookie。 Response.Cookies.Add(cookieEdit); //如果有主站及二级域名站且cookie要共享的话则要加入如下设置 cookie.Domain = ".cnblog.com"; cookie.Path = "/"; public static void AddCookie(string key,string value,int expires) { var acookie = new HttpCookie(key); acookie.Value = value; acookie.Domain = ".cnblog.com"; acookie.Path = "/"; acookie.Expires = DateTime.Now.AddMinutes(expires); HttpContext.Current.Response.Cookies.Add(acookie); } public static string GetCookie(string key) { var httpCookie = HttpContext.Current.Request.Cookies[key]; if (httpCookie != null) { return httpCookie.Value; } return ""; }
登录记住用户名和密码示例
<body> <form id="form1" method="post" action="rembPage.aspx"> <div> 帐号: <input type="text" name="userName" /><br /> 密码 :<input type="password" name="pass" /><br /> 记住我: <input type="checkbox" value="rem" name="sele1" /><br /> <input type="submit" value=" 登录 " /> </div> </form> </body> if (Request.Cookies["userName"] == null && Request.Cookies["passWord"] == null) //判断是否存在cookie,如果存在表示上次选择了记住我 { if (Request.Form["userName"] != null && Request.Form["pass"] != null) { String userName = Request.Form["userName"]; String userPassWord = Request.Form["pass"]; if (userName == "admin" && userPassWord == "123") { if (Request.Form["sele1"] != null) { HttpCookie cookieUserName = new HttpCookie("userName", userName); //创建帐号的cookie实例 HttpCookie cookiePassWord = new HttpCookie("passWord", userPassWord); cookieUserName.Expires = DateTime.Now.AddDays(2); //设置帐号cookie的过期时间,当前时间算往后推两天 cookiePassWord.Expires = new DateTime(2012, 5, 27); //设置密码cookie的过期时间,过期时间为2012年5月27日 Response.Cookies.Add(cookieUserName); //将创建的cookieUserName文件输入到浏览器端 Response.Cookies.Add(cookiePassWord); Response.Redirect("Index.aspx"); //跳转到你想要的页面 } else { Response.Redirect("Index.aspx");//即便不记住密码也要跳转 } } } } else { Response.Redirect("Index.aspx");//如果记住密码,第二次登录将直接进入Index.aspx页面 }