在ASP.NET 2.0中向表呈现元组的简单方法?
假设我有一个类似于在ASP.NET 2.0中向表呈现元组的简单方法?,asp.net,Asp.net,假设我有一个类似于字典或类似的数据结构,我希望将其呈现为一个HTML表,其中行标题作为第一个字符串键,列标题作为第二个字符串键。是否有用于此的内置控件或其他控件?没有内置控件可以识别这样复杂的数据结构。您需要为它做一些自定义编码 使用带有ItemDataBound事件处理程序的中继器可能很容易实现这一点。就在我的头顶上,没有测试: <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound=" Repeater1_OnItemD
字典
或类似的数据结构,我希望将其呈现为一个HTML表,其中行标题作为第一个字符串键,列标题作为第二个字符串键。是否有用于此的内置控件或其他控件?没有内置控件可以识别这样复杂的数据结构。您需要为它做一些自定义编码
使用带有ItemDataBound事件处理程序的中继器可能很容易实现这一点。就在我的头顶上,没有测试:
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound=" Repeater1_OnItemDataBound">
<ItemTemplate>
<asp:Literal ID="Literal1" runat="server" />
</ItemTemplate>
</asp:Repeater>
protected void Repeater1_OnItemDataBound(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
var rowHeader= (e.Item.DataItem).Key;
var columnHeaders = (e.Item.DataItem).Value;
foreach (var header in columnHeaders)
{
// build string to populate table row, assign to Literal1
}
}
}
受保护的无效Repeater1\u OnItemDataBound(对象发送方,RepeaterItemEventArgs e)
{
如果(e.Item.ItemType==ListItemType.Item | | e.Item.ItemType==
ListItemType.AlternatingItem)
{
var rowHeader=(e.Item.DataItem).Key;
var columnHeaders=(e.Item.DataItem).Value;
foreach(ColumnHeader中的var标头)
{
//生成字符串以填充表行,分配给Literal1
}
}
}
您可以使用嵌套的数据绑定控件并跳过OnItemDataBoundStep,只需将内部控件与要绑定的外部项的属性绑定即可
因此,对于您的情况,字典中的每个项都包含一个键和另一个字典。因此,每次绑定时,您都可以访问字典,以便将该项设置为内部数据绑定控件的数据源
考虑以下示例:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Collections.Generic" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Dictionary<string, Dictionary<string, int>> TestDict = new Dictionary<string, Dictionary<string, int>>();
//This is just loading the test dictionary
for (int i = 0; i < 10; i++)
{
Dictionary<string, int> ColData = new Dictionary<string, int>();
TestDict.Add("Row Header " + i, ColData);
for (int j = 0; j < 5; j++)
{
ColData.Add("Col Header " + j, i + j);
}
}
//Bind the Outer Repeater
RepeaterRow.DataSource = TestDict;
RepeaterRow.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form id="form1" runat="server">
<asp:Repeater ID="RepeaterRow" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<th>
<%# Eval("Key") %>
</th>
<asp:Repeater ID="RepeaterColumn" DataSource='<%# Eval("Value")%>' runat="server">
<ItemTemplate>
<td>
<%# Eval("Value") %>
</td>
</ItemTemplate>
</asp:Repeater>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
受保护的无效页面加载(对象发送方、事件参数e)
{
Dictionary TestDict=新字典();
//这只是加载测试字典
对于(int i=0;i<10;i++)
{
Dictionary ColData=newdictionary();
添加(“行标题”+i,ColData);
对于(int j=0;j<5;j++)
{
ColData.Add(“列标题”+j,i+j);
}
}
//绑定外部中继器
RepeaterRow.DataSource=TestDict;
RepeaterRow.DataBind();
}
或者,我可以从数据库中以类似“行、列、值”的格式获取数据,如果这样做更简单的话……我最终构建了一个简单的控件,该控件具有“setData(行、列、值)”方法并呈现到HTML表中。是否有一个控件可以识别另一种类型的数据结构来保存这种类型的数据?像上面提到的SQL结果(行、列、值)?我不知道。您可以使用gridview来识别数据的DataRow样式,因此,如果可以将元组放入DataTable中,则可以轻松地绑定到该表并让HeaderTemplate处理标题,并且行标题可以位于第一列中。但据我所知,没有现成的电子表格样式的控件可以像这样解析数据。