我引用了Ajaxpro。function checkUser()
{
     var username=document.getElementById("<%= UserName.ClientID %>").value;
     var PassWord=document.getElementById("<%= PassWord.ClientID %>").value;
     var exists = Picascx_Login.checkUser(username,"51aspx").value;
   if(exists)
   {
       // document.getElementById("Text1").value='<%=HttpContext.Current.Session["UserName"]%>'//这里就是问题所在,不刷新的话Text1就一直为空
       // document.getElementById("Text2").value='<%=HttpContext.Current.Session["Point"]%>'
        document.getElementById("tblogin").style.display="none";
        document.getElementById("tbshow").style.display="";
       // window.history.go(0);
       return true;
   
   }
   else
   {
        alert('用户名或密码错误!');
        return false;
   }
}
这是我的js
 [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
    public bool checkUser(string userName, string password)
    {
        SqlParameter[] prams ={db.ParamBuilder("@USERNAME",userName,SqlDbType.VarChar),
            db.ParamBuilder("@PASSWORD",md5Password(password),SqlDbType.VarChar)
        };
        SqlDataReader dr = db.DataReader("Pr_Mp4_CheckUser", prams);
        if (dr.Read())
        {
           HttpContext.Current.Session["UserID"] = dr["UserID"].ToString();
           HttpContext.Current.Session["UserName"] = dr["UserName"].ToString();
           HttpContext.Current.Session["Point"] = dr["Point"].ToString();
           Page.ClientScript.RegisterStartupScript(Page.GetType(), "key2", "<script> document.getElementById('Text1').value= '"+dr["UserName"].ToString()+"';</script>");
           dr.Dispose();
           return true;
          //  Page.ClientScript.RegisterStartupScript(Page.GetType(), "key2", "<script>window.history.go(-1)</script>");
        }
        else
        {
        //    Page.ClientScript.RegisterStartupScript(Page.GetType(), "key3", "<script>alert('用户名或密码错误!')</script>");
            return false;
        }
    }
这是我的后台代码,
// document.getElementById("Text1").value='<%=HttpContext.Current.Session["UserName"]%>'//这里就是问题所在,不刷新的话Text1就一直为空
请教大家我应该用什么方法判断了登陆之后,然后立刻从session里取值,而且显示出来呢?

解决方案 »

  1.   

    再在后台加方法js调用获取session的值,页面载入以后js是得不到服务器的数据的,只有再次通过ajax请求获取数据.或者在第一次判断的时候可以返回相关的数据
      

  2.   


    #region   设置Session值
    /**//// <summary>
    [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
    public void WriteSession(string str)
    {
       Session["UserAccount"] = str;
    }
    #endregion#region 获取Session值     
    [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
    public string ReadSession()
    {
        string str = "";
        if (Session["UserAccount"] != null)
        {
            str = Session["UserAccount"].ToString();
        }
        return str;
    }
    #endregion
      

  3.   

    使用AjaxPro.HttpSessionStateRequirement.ReadWrite
    页面值设置后要清空或刷新才清除