【问题标题】:How to view list of fellow classmates in GridView?如何在GridView中查看同学列表?
【发布时间】:2013-12-30 17:43:24
【问题描述】:

我是 C# 编程的初学者,我被分配了一项社交网络任务。 我已经让数据库正常工作,我必须提供一个页面以允许用户查看他/她班级的学生姓名列表。

这是我的数据库大致的样子:

表格名称:studentTable

name  |  class
------+---------
John  |  classA
Tim   |  classB
Kate  |  classA
Max   |  classA

例如,如果 John 登录网站,他将能够在 GridView 中将 Kate 和 Max 视为他的同学,而不是 Tim。

这是我的 SqlDataSource 代码:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:StudentCS %>" 
    SelectCommand="SELECT [name], [class] FROM [studentTable] WHERE ([class] = @class)">
    <SelectParameters>
        <asp:Parameter Name="class" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

我可以知道如何使用 GridView 或任何其他方式来完成此操作吗?

【问题讨论】:

    标签: c# asp.net sql-server gridview social


    【解决方案1】:

    您是否已将您的GridView 包含到您的 .aspx 页面中?如果没有,

    <asp:GridView ID="GridView1"  DataSourceID="SqlDataSource1"  
    AutoGenerateColumns="true" runat="server">
    </asp:GridView>
    

    注意:您将需要某种登录机制,以便您可以识别哪些用户登录,以便您可以查询数据库以获取他/她的类作为您的参数传递。

    【讨论】:

    • OP可以使用User.Identity.Name获取登录用户名。
    【解决方案2】:

    试试下面这可能会帮助你解决它。您必须将登录的学生姓名保存在会话中 i-e) Session["StudentName"]

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:StudentCS %>" 
        SelectCommand="SELECT [name], [class] FROM [studentTable] AS A , [studentTable] AS B WHERE A.Class = B.Class AND A.name = @StudentName ">
        <SelectParameters>
            <asp:SessionParameter SessionField="StudentName" Name="StudentName" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-15
      • 1970-01-01
      • 1970-01-01
      • 2018-04-25
      • 2011-11-19
      • 1970-01-01
      • 1970-01-01
      • 2012-06-18
      相关资源
      最近更新 更多