[英]Getting current logged in user to display it records in gridview in ASP.NET C#
I'm trying to display the current logged in user and its record in gridview.我正在尝试在 gridview 中显示当前登录的用户及其记录。 If a user logged in he can view all of his records and tables associated to him.
如果用户登录,他可以查看与他关联的所有记录和表格。 Here's a simple code of mine.
这是我的一个简单代码。
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="Account.aspx.cs" Inherits="LibrarySystem.Member.MyAccount" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<h3>
My Account</h3>
<p>
</p>
<p>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="lenid" DataSourceID="useraccountDataSource"
ForeColor="#333333" GridLines="None">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField DataField="lenid" HeaderText="Lend ID" InsertVisible="False"
ReadOnly="True" SortExpression="lenid" />
<asp:BoundField DataField="bookid" HeaderText="Book ID/ISBN"
SortExpression="bookid" />
<asp:BoundField DataField="EmployeeID" HeaderText="Employee ID"
SortExpression="EmployeeID" />
<asp:BoundField DataField="department" HeaderText="Department"
SortExpression="department" />
<asp:BoundField DataField="dateborrowed" HeaderText="Date borrowed"
SortExpression="dateborrowed" />
<asp:BoundField DataField="datereturned" HeaderText="Date returned"
SortExpression="datereturned" />
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:SqlDataSource ID="useraccountDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
SelectCommand="SELECT [lenid], [bookid], [EmployeeID], [department], [dateborrowed], [datereturned] FROM [LendTable] WHERE ([EmployeeID] = @EmployeeID)">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="EmployeeID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</p>
<p>
</p>
</asp:Content>
EmployeeID is the same as the Username (I've pointed the aspnet user database to my own database). EmployeeID 与用户名相同(我已将 aspnet 用户数据库指向我自己的数据库)。 I wanted to display logged in user's record only to the current logged in user.
我想仅向当前登录用户显示登录用户的记录。
Any help would be much appreciated;) Thanks in advance.任何帮助将不胜感激;)提前致谢。
Since you said in the question that the employeeId is the Username
, you can access the logged in Username as follows:由于您在问题中说
employeeId is the Username
,您可以按如下方式访问登录的 Username :
HttpContext.Current.User.Identity.Name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.