ASP.NET记住登陆用户名的具体实现
更新时间:2013年06月12日 14:06:47 作者:
ASP.NET记住登陆用户名的具体实现,需要的朋友可以参考一下
.aspx文件中
复制代码 代码如下:
…
<asp:TextBox ID="txtUser_Id" runat="server" MaxLength="4" Width="120px" BorderColor="LightSlateGray" BorderWidth="1px"></asp:TextBox>
…
<asp:ImageButton ID="btnInsert" runat="server" ImageUrl="~/Images/Login.GIF" OnClick="btnInsert_Click" />
…
<asp:CheckBox ID="cbxRemeberUser" runat="server" Text="记住用户名" Font-Size="Small" ForeColor="gray"/>
…
.aspx.cs文件中
复制代码 代码如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.txtUser_Id.Focus();
if (!Object.Equals(Request.Cookies["UserID"], null))
{
//创建一个Cookie对象,实现记住用户名
HttpCookie readcookie = Request.Cookies["UserID"];
this.txtUser_Id.Text = readcookie.Value;
}
}
}
private void CreateCookie()
{
//创建一个Cookie对象
HttpCookie cookie = new HttpCookie("UserID");
//判断Checkbox控件是否被选中
if (this.cbxRemeberUser.Checked)
{
//将用户编号存储到创建的Cookie对象中
cookie.Value = this.txtUser_Id.Text;
}
//获取创建的Cookie对象的过期时间
cookie.Expires = DateTime.MaxValue;
//将创建的Cookie对象添加到内部Cookie集合中
Response.AppendCookie(cookie);
}
protected void btnInsert_Click(object sender, ImageClickEventArgs e)
{
…
if (object.Equals(Request.Cookies["UserID"], null))
{
//调用自定义方法 CreateCookie()存储用户名
CreateCookie();
}
else
{
CreateCookie();
}
…
}
相关文章
ASP.NET服务器端控件RadioButtonList,DropDownList,CheckBoxList的取值、赋值
这三个控件都有一个Items集合,可以用 RepeatLayout 和 RepeatDirection 属性来控制列表的呈现形式2013-10-10
ASP.NET Core中使用MialKit实现邮件发送功能
这篇文章主要介绍了ASP.NET Core中使用MialKit实现邮件发送功能,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下2019-10-10


最新评论