[英]How do I display specific user details for my logged in user?
Below is the code I am trying to implement. 以下是我尝试实现的代码。
I want to return data for the user logged in, when I run a listview
using this datasource
without WHERE UserName = @UserName
I get a full list of my users. 当我使用不带
WHERE UserName = @UserName
该datasource
运行listview
时,我想返回登录用户的数据,我得到了我的用户的完整列表。
However, I want only to return the logged in users. 但是,我只想返回已登录的用户。 With
WHERE UserName = @UserName
and my Select Parameters set I get null data returned - how do I change my SELECT
statement to select data for my logged in user? 设置
WHERE UserName = @UserName
并设置我的Select Parameters后,我将返回空数据-如何更改SELECT
语句为登录用户选择数据?
<asp:sqldatasource
id="SqlDataSource1"
runat="server"
connectionstring="<%$ ConnectionStrings:SecondConnectionString %>"
selectcommand="SELECT vw_aspnet_MembershipUsers.UserId, vw_aspnet_MembershipUsers.Email, vw_aspnet_MembershipUsers.CreateDate, vw_aspnet_MembershipUsers.LastLoginDate, vw_aspnet_MembershipUsers.UserName, vw_aspnet_MembershipUsers.LastActivityDate
WHERE UserName = @UserName">
<SelectParameters>
<asp:Parameter name="UserName" type="String" DefaultValue="LoggedInUser" />
</SelectParameters>
</asp:sqldatasource>
Your select parameter is probably never set. 您的选择参数可能从未设置。 I suggest that you try to set the default value to something that exists in your database.
我建议您尝试将默认值设置为数据库中存在的值。 If that makes a difference, you'll know that the parameter is never set to anything other than the default.
如果这有所作为,您将知道该参数绝不会设置为默认值。
You can set the value of the parameter by using a controlparameter instead: 您可以改为使用controlparameter来设置参数的值:
<SelectParameters>
<asp:ControlParameter ControlID="__Page" Name="UserName" PropertyName="LoggedInUser" Type="String" />
</SelectParameters>
This will get the username from a property called LoggedInUser in the codebehind. 这将从隐藏代码中名为LoggedInUser的属性中获取用户名。
public string LoggedInUser
{
get
{
return "RJoness";
}
}
Or you can set the parameter directly from codebehind: 或者,您可以直接从代码背后设置参数:
SqlDataSource1.SelectParameters.Add("UserName", "RJoness");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.