【发布时间】:2019-12-06 13:51:39
【问题描述】:
假设我有一些这样的图书数据:
我有一个类似SELECT * FROM BookData 的查询,它以上述格式输出。
我想使用嵌套转发器控件以 html <table> 的形式输出数据,看起来像这样,数据结果按作者分组:
到目前为止,我的 asp.net 网络表单代码如下所示:
<table>
<asp:Repeater ID="RepeaterInner" runat="server">
<ItemTemplate>
<tr>
<td><asp:TextBox Text='<%# Eval("Book") %>' /></td>
<td><asp:TextBox Text='<%# Eval("Author") %>' /></td>
<td><asp:TextBox Text='<%# Eval("PublishDate") %>' /></td>
<td><asp:TextBox Text='<%# Eval("Pages") %>' /></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
还有我的后台代码:
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = GetData();
RepeaterInner.DataSource = ds;
RepeaterInner.DataBind();
}
private DataSet GetData()
{
string CS = ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
using (SqlCommand cmd = new SqlCommand("spGetData"))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
return ds;
}
}
}
对于最终产品,我希望它看起来像这样:
<table>
<asp:Repeater ID="RepeaterOuter" runat="server">
<ItemTemplate>
<tr>
<td><asp:TextBox Text='<%# Eval("Author") %>' /></td>
</tr>
<asp:Repeater ID="RepeaterInner" runat="server">
<ItemTemplate>
<tr>
<td><asp:TextBox Text='<%# Eval("Book") %>' /></td>
<td><asp:TextBox Text='<%# Eval("PublishDate") %>' /></td>
<td><asp:TextBox Text='<%# Eval("Pages") %>' /></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</table>
但我真的很困惑如何才能实现上述目标。可以用我的一个存储过程生成的上面的DataSet 来完成吗?如果有的话,我需要为每个中继器的 ItemDataBound 事件处理程序做什么?
谢谢。
【问题讨论】: