如何制作金字塔形状的asp.net datalist?

时间:2017-09-25 19:21:00

标签: c# asp.net database linq datalist

这是我的代码隐藏。

 public void pyramidlist()
    {
        SqlConnection path= new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True");
        path.Open();
        SqlDataAdapter da = new SqlDataAdapter("Select * from Players", yol);
        DataTable dt = new DataTable();
        da.Fill(dt);



        List<Players> playerList= new List<Players>();
        playerList= (from DataRow dr in dt.Rows
                         select new Players()
                         {
                             Name= (dr["Name"].ToString()),
                             Order= int.Parse(dr["Order"].ToString()),
                             ID = int.Parse(dr["ID"].ToString())

                         }).ToList();

        playerList= playerList.OrderBy(x => x.ID).ToList();


        datalist1.DataSource = playerList;
        datalist1.DataBind();

public class Players
    {
        public string Name{ get; set; }
        public int Order{ get; set; }
        public int ID { get; set; }
    }

我想设置RepeatColums属性1.2.3.4.5.6.7.8.9.10。最后,datalist将显示为triangel或金字塔。 你可以向我展示不同的方式。没问题。我想改变RepeatColums。首先记录第二行的第一行,第二和第三条记录,第三行记录4-5-6条记录。

Default.aspx Code ...

<asp:DataList runat="server" ID="datalist1" RepeatLayout="Flow" RepeatColumns="2"> <ItemTemplate>

            <asp:HyperLink ID="HyperLink1" CssClass="link" NavigateUrl='PlayerDetails.aspx?id=<%# Eval("Id") %>' runat="server"> <%# Eval("Sira") %>. <%# Eval("AdSoyad") %></asp:HyperLink>

          </ItemTemplate> </asp:DataList>

1 个答案:

答案 0 :(得分:1)

如果嵌套两个datalist元素,可以使用外部循环来控制内部循环的长度

因此,后面的代码类似,但包含一个容器列表,用于定义重复的长度

protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection path = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True");
        path.Open();
        SqlDataAdapter da = new SqlDataAdapter("Select * from Players", path);
        DataTable dt = new DataTable();
        da.Fill(dt);


        // fill the players list from the database
        List<Players> playerList = new List<Players>();
        playerList = (from DataRow dr in dt.Rows
                      select new Players()
                      {
                          Name = (dr["Name"].ToString()),
                          Order = int.Parse(dr["Order"].ToString()),
                          ID = int.Parse(dr["ID"].ToString())

                      }).ToList();

        playerList = playerList.OrderBy(x => x.ID).ToList();

        // consume the players list in increasing increments
        List<Tier> tierList = new List<Tier>();
        for(var tierLength = 1; playerList.Count > 0; tierLength++)
        {
            var tier = new Tier();
            tier.Players = new List<Players>();
            tier.TierLength = tierLength;
            for(var playerCount = 0; playerCount < tierLength && playerList.Count > 0; playerCount++)
            {
                tier.Players.Add(playerList[0]);
                playerList.RemoveAt(0);
            }
            tierList.Add(tier);
        }

        // bind the tierList to the outer datalist element
        // the inner datalist element will be a child
        DataList0.DataSource = tierList;
        DataList0.DataBind();
    }
}
public class Players
{
    public string Name { get; set; }
    public int Order { get; set; }
    public int ID { get; set; }
}
public class Tier
{
    public List<Players> Players { get; set; }
    public int TierLength { get; set; }
}

并且ASPX代码看起来像这样

<asp:DataList ID="DataList0" runat="server">
    <ItemTemplate>
        <asp:DataList ID="DataList1" runat="server" DataSource='<%# Eval("Players") %>' RepeatLayout="Flow" RepeatColumns='<%# Eval ("TierLength") %>'>
            <ItemTemplate>
                <asp:HyperLink ID="HyperLink1" CssClass="link" NavigateUrl='PlayerDetails.aspx?id=<%# Eval("ID") %>' runat="server"><%# Eval("Name") %></asp:HyperLink>
            </ItemTemplate>
        </asp:DataList>
    </ItemTemplate>
</asp:DataList>