今天做登录时,遇到点小问题,在网上找了一下,没看到源码案例,不过还是花了一点时间调试通过了

在此记录一下,备忘。

主要是Request.UrlReferrer的用法

注意:   如果上一页面使用document.location方法导航到当前页面,Request.UrlReferrer返回空值
           如果有A,B两个页面,在浏览器中直接请求A页面,在A页面的中Page_Load事件中导航到B 页面,

则    Request.UrlReferrer返回空。因为 在Page_load事件中页面还未初始化,所以无法记录当前页的信息,

导航到b页面也就无法获得上一页面的信息   

 

  protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.UrlReferrer != null)
                {
                    ViewState["UrlReferrer"] = Request.UrlReferrer.ToString();
                }
                if (Session["user"] != null)
                {
                    if (Request.UrlReferrer != null)
                    {
                        Response.Redirect(Request.UrlReferrer.ToString());
                    }
                    else
                    {
                        Response.Redirect("/");
                    }
                }
                username.Value = Request.Form["Uname"];
                pass.Attributes.Add("value", Request.Form["password"]);
            }
        }
        /// <summary>
        /// 个人会员登陆
        /// </summary>
        protected void userLog()
        {
            Lovetrip.BLL.Manage.Users bllu = new Lovetrip.BLL.Manage.Users();
            Lovetrip.Model.Manage.Users modeu = bllu.Login(username.Value.Trim(), pass.Text.Trim());
            if (modeu != null)
            {
                Session["user"] = modeu;
                Session["userType"] = 1;
                Command.Public.MoveUserSession(1);
                bbsLogin(modeu.unick);
                if (Request.UrlReferrer != null)
                {
                    Response.Redirect(ViewState["UrlReferrer"].ToString());
                }
                else
                {
                    Response.Redirect("/");
                }

            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('帐号或密码输入有误!')", true);
            }
        }