我正在使用asp.net并希望动态生成多个表,然后在网页上显示每个表。所以
在Environments.aspx.cs文件中:
可能在Environments.aspx文件中:
我想避免创建一个大表来维护用户友好的外观。如果有人可以提供一个简短的代码示例,我会很感激。
答案 0 :(得分: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();
}
答案 1 :(得分:1)
不确定我是否得到了你的要求。 我在你的评论中看到你有1个选择命令。 另外,你能举例说明它应该是什么样的吗?
如果你想要一种方法来获得所有表看起来相同的东西,你可以尝试使用转发器。
编辑:使用gridview可能比表格更容易,因为您只需要将ItemDataBound数据集绑定到转发器中的gridview。
Environments.aspx:
<asp:Repeater ID="repeatExample" runat="server" OnItemDataBound="repeatExample_ItemDataBound">
<ItemTemplate>
//Place table here
</ItemTemplate>
</asp:Repeater>
Environments.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);
[...]
}
希望得到这个帮助。
P.S。我没有尝试List of DataSet。它可能不起作用。