【问题标题】:How get data from database column as a heading using Repeater in ASP.NET如何在 ASP.NET 中使用 Repeater 从数据库列获取数据作为标题
【发布时间】:2018-03-28 18:03:33
【问题描述】:

image 上半部分显示我的数据库表结构,下半部分显示我想以哪种形式显示数据。

<table class="table table-bordered table-hover">
  <thead>
    <tr class="text-center">
      <th>Course Code</th>
      <th>Subject</th>
      <th>Cr.Hours</th>
      <th>Grade</th>
    </tr>
    <tr>
      <td colspan="4">
        <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
      </td>
    </tr>
  </thead>
  <tbody>

    <asp:Repeater ID="outer" runat="server">

      <ItemTemplate>
        <tr>
          <td>
            <asp:Label Text='<%#Eval("[SpringFall]")%>' Style="text-align: center; display: none; font-size: 18px; font-family: 'Times New Roman'" ID="Label4" runat="server" Font-Bold="true"> </asp:Label>

          </td>
          <td>
            <asp:Label ID="AllCcode" runat="server" Text='<%#Eval("[Course_Code]") %>'></asp:Label>
          </td>
          <td>
            <asp:Label ID="AllSubject" runat="server" Text='<%#Eval("[Subject]") %>'></asp:Label>
          </td>
          <td>
            <asp:Label ID="AllCrHr" runat="server" Text='<%#Eval("[Credit_Hours]") %>'></asp:Label>
          </td>
          <td>
            <asp:Label ID="AllGrade" Font-Bold="true" runat="server" Text='<%#Eval("[Total_Marks]") %>'></asp:Label>
          </td>
        </tr>
      </ItemTemplate>
    </asp:Repeater>
  </tbody>
</table>

这是我的代码..使用 c# 和简单的 sql SELECT 查询在按钮单击事件上绑定转发器。绑定转发器的任何查询都具有更高效的工作并以给定的形式显示数据。

protected void AttendencBtn_Click(object sender, EventArgs e)
{
  //  MultiView1.ActiveViewIndex = 8;
    conn.Open();
    sda = new SqlDataAdapter("SELECT * FROM  Attendence where Roll_Number='" + Session["RollNumb"] + "' ", conn);
    dt = new DataTable();
    sda.Fill(dt);
    AttendencRpt.DataSource = dt;
    AttendencRpt.DataBind();
    conn.Close();
}

【问题讨论】:

  • 你想要什么?
  • 在性能方面DataReader 总是比DataAdapter
  • 查看图片以获得澄清。Repeater 绑定数据库中的所有数据,但我想要 .a 列,每行中具有相同数据的列 ..分组并立即显示在标签上 ..使用中继器 ..
  • 我看过你的图片但还是不明白?你指出了 2014 年和 2015 年。那么,你想达到什么目标,你能用语言解释一下吗?
  • 这是一个很好的起点。 stackoverflow.com/help/how-to-ask

标签: c# asp.net sql-server asprepeater


【解决方案1】:

我认为您正在寻找类似的东西。首先在后面的代码中创建一个公共变量。

public int currentYear = 2000;

protected void Page_Load(object sender, EventArgs e)
{
}

然后把Repeater改成这样:

<table border="1">
    <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>

            <%# Convert.ToDateTime(Eval("myYear")).Year != currentYear && Container.ItemIndex > 0 ? "<tr><td colspan=\"5\">" + Eval("myYear") + "</td></tr>" : "" %>

            <tr>
                <td>
                    normal rows here
                </td>
            </tr>

            <%# currentYear = Convert.ToDateTime(Eval("myYear")).Year %>

        </ItemTemplate>
    </asp:Repeater>
</table>

将发生的情况是将currentYear 的值与当前行值进行比较。如果不匹配,将创建一个新行。然后currentYear 的值被更新以在绑定到Repeater 的下一行中进行检查。

【讨论】:

  • Thnx for Suggestion near my question很高兴你理解这个问题
  • 此列中的数据 秋季(2014) 秋季(2014) 春季(2015) 春季(2015) 秋季(2015) 秋季(2015) 春季(2016) 秋季(2016) 春季(2017)秋季(2017)如何绑定中继器一次获取此数据。
  • 您必须确保 currentYear 成为在中继器中拆分数据的分隔符。因此,数据集中必须有一列充当分隔符,例如 DateTime 字段。
猜你喜欢
  • 2018-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-03
  • 1970-01-01
  • 1970-01-01
  • 2017-09-20
相关资源
最近更新 更多