从SqlDataSource设置会话变量

时间:2014-12-05 21:55:34

标签: c# asp.net session session-variables

我正在尝试从SqlDataSource的返回设置一些会话变量。我能够将值设置为标签/文本框/隐藏字段,但我似乎无法设置会话变量。

我正在使用page_load函数来尝试设置它们。

protected void Page_Load(object sender, EventArgs e)
{        
        this.Session["UserName"] = User.Identity.Name;
        this.Session["EmplID"] = Eval("EmplID");
}

我有SqlDataSource

<asp:SqlDataSource ID="SQLGetEmplID" runat="server" 
            ConnectionString="<%$ ConnectionStrings:MAFapp %>" 
            SelectCommand="SELECT DISTINCT EmplID, Status FROM Employees WHERE (intLogin = @UserName) AND (Dept = N'Eng' OR Dept = N'IS') AND (Status = N'Active')">
    <SelectParameters>
        <asp:SessionParameter Name="UserName" SessionField="UserName" />
    </SelectParameters>
</asp:SqlDataSource>

1 个答案:

答案 0 :(得分:0)

如果您的User.Identity.Name

,则可以替代您正在做的事情
string fullUserName = null;
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain))
{
    using (UserPrincipal user = UserPrincipal.FindByIdentity(pc,User.Identity.Name))
    {
        if (user != null)
        {
            fullUserName = user.DisplayName;//this can be what ever you like 
        }
    }
}