简体   繁体   English

如何在ASP.NET中使用Repeater从数据库列获取数据作为标题

[英]How get data from database column as a heading using Repeater in ASP.NET

image The Upper portion of present my database table structure and lower portion that in which form i want to show data. 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>

Here Is my code..bind repeater on button click event using c# with simple sql SELECT query.Any query for bind repeater with more efficient working and show data in given form. 这是我的代码..使用简单的SQL SELECT查询使用c#在按钮单击事件上绑定中继器。任何查询绑定中继器的工作效率更高,并以给定的形式显示数据。

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();
}

I think you are looking for something like this. 我认为您正在寻找类似的东西。 First create a public variable in code behind. 首先在代码后面创建一个公共变量。

public int currentYear = 2000;

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

Then change the Repeater to something like this: 然后将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>

What will happen is that the value of currentYear is compared to the current row value. 将会发生的是将currentYear的值与当前行的值进行比较。 If it does not match a new row will be created. 如果不匹配,将创建新行。 Then the value of currentYear is updated to be checked in the next row bound to the Repeater. 然后, currentYear的值将更新,以在绑定到Repeater的下一行中进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM