2

我正在使用 asp.net 并希望动态生成多个表,然后在网页上显示每个表。所以

在 Environments.aspx.cs 文件中:

  1. 查询数据库以获取数据并发现应该创建多少表
  2. 使用循环创建和定义每个表(无论是 1 还是 50)。每个表有 6 列,行取决于返回的数据

可能在 Environments.aspx 文件中:

  1. 显示创建的每个表。我应该完全控制桌子的位置。

我想避免创建一个大表来保持用户友好的外观。如果有人可以提供一个简短的代码示例,我将不胜感激。

4

2 回答 2

3

这是一个示例。我希望它能让你继续前进。

在此处输入图像描述

<asp:ListView runat="server" ID="ListView1" ItemPlaceholderID="Panel1" 
    OnItemDataBound="ListView_ItemDataBound">
    <LayoutTemplate>
        <asp:Panel runat="server" ID="Panel1"></asp:Panel>
    </LayoutTemplate>
    <ItemTemplate>
        <%#Eval("Name") %>
        <asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="LastName" HeaderText="LastName" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
            </Columns>
        </asp:GridView>
    </ItemTemplate>
</asp:ListView>

public class House
{
    public int HouseId { get; set; }
    public string Name { get; set; }
    public List<User> Users { get; set; }
}

public class User
{
    public int UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public List<House> Houses
{
    get
    {
        return new List<House>
            {
                new House
                    {
                        HouseId = 1,
                        Name = "House One",
                        Users = new List<User>()
                            {
                                new User {UserId = 1, FirstName = "John", LastName = "Newton"},
                                new User {UserId = 2, FirstName = "Marry", LastName = "Newton"},
                                new User {UserId = 3, FirstName = "Joe", LastName = "Newton"}
                            }
                    },
                new House
                    {
                        HouseId = 1,
                        Name = "House Two",
                        Users = new List<User>()
                            {
                                new User {UserId = 6, FirstName = "Newton", LastName = "Doe"},
                                new User {UserId = 7, FirstName = "Jack", LastName = "Doe"},
                                new User {UserId = 8, FirstName = "Lewis", LastName = "Doe"}
                            }
                    }
            };
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ListView1.DataSource = Houses;
        ListView1.DataBind();
    }
}

protected void ListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    var house = e.Item.DataItem as House;
    var gridView = e.Item.FindControl("GridView1") as GridView;

    gridView.DataSource = house.Users;
    gridView.DataBind();
}
于 2013-02-20T21:28:27.753 回答
1

不知道我是否明白你的要求。我在您的评论中看到您有 1 个选择命令。否则,你能举例说明它应该是什么样子吗?

如果您想要一种方法来获得所有表格看起来都相同的东西,您可以尝试使用中继器。

编辑:使用网格视图可能比表格更容易,因为您只需在 ItemDataBound 中将数据集绑定到中继器中的网格视图。

环境.aspx:

<asp:Repeater ID="repeatExample" runat="server" OnItemDataBound="repeatExample_ItemDataBound">
     <ItemTemplate>
          //Place table here
     </ItemTemplate>
</asp:Repeater>

环境.aspx.cs:

private void bindRepeater()
    {
        //Get all your info using a stored procedure
        //ExampleClassDAL being where my code to call the stored proc is and return a 
        //List of DataSets (You would have to code a way to get multiple dataset for each table in your page.)
        List<DataSet> dsExample = ExampleClassDAL.StoredProcGetAllTheStuff();
        //Add extra code here if needed
        [...]
        //Bind the repeater
        repeatTournois.DataSource = dsExample ;
        repeatTournois.DataBind();
    }

 protected void repeatExample_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
       //Fill your table here using the DataSource
       DataSet ds = ((DataSet)e.Item.DataItem);
       [...]
    }

希望这有帮助。

PS我没有尝试数据集列表。它可能不起作用。

于 2013-02-20T21:17:13.980 回答