0

我正在尝试从 loginView 获取用户名值。我从这里和其他网站尝试了很多方法。有些不起作用,有些只给我在计算机上或从 AD 登录。

有谁知道,如何在网站上获取登录人的用户名?

我试过这段代码:

LoginName loginName = HeadLoginView.FindControl("HeadLoginName") as LoginName;
        Label1.Text = (loginName.ToString());

所以也在这里尝试了这段代码:

 //var Login1 = ((Login)HeadLoginView.FindControl("HeadLoginName"));

        //var Login1 = ((LoginName)HeadLoginView.FindControl("HeadLoginName"));

        //LoginName Login1 = ((LoginName)HeadLoginView.FindControl("HeadLoginName"));
        //string userName = Login1.UniqueID.ToString();

        //string Username = User.Identity.Name;
        //Label1.Text = Username;

        //LoginName Login1 = ((LoginName)HeadLoginView.FindControl("HeadLoginName"));
        //string userName = Convert.ToString(Login1);

        //Login Login1 = ((Login)HeadLoginView.FindControl("HeadLoginName"));
        //string userName = Login1.UserName.ToString();

        //var userName = ((Login)HeadLoginView.FindControl("HeadLoginName"));
        //Label1.Text = Convert.ToString(userName);

        //LoginName loginName = (LoginName)HeadLoginView.FindControl("HeadLoginName");
        //Label1.Text = Convert.ToString(loginName);

        //string merda = (HttpContext.Current.User.Identity.Name);

        //LoginName loginName = HeadLoginView.FindControl("HeadLoginName") as LoginName;
        //Label1.Text = (loginName.ToString());  

但总是相同的错误,nullexeption 和 'System.Web.UI.WebControls.LoginName' ao Tipo 'System.Web.UI.WebControls.Login

但没有成功

4

2 回答 2

1

这里有两种可能的方法:

1) 尝试从 LoginView 中获取 LoginName,如下所示:

LoginName loginName = (LoginName)HeadLoginView.FindControl("HeadLoginName");

2)或者你可以浏览到这个属性:

HttpContext.Current.User.Identity.Name

干杯

于 2012-08-20T18:52:40.980 回答
0

采用:

Login login2 = (Login)(LoginView1.FindControl("Login2"));

if(login2!=null) {
    string user=login2.UserName;
    string pass=login2.Password;
}
于 2016-02-24T08:13:03.783 回答