在 table 中填充嵌套中继器
Fill nested repeaters in a table
我在另一个中继器中有一个中继器。如何在内部转发器中显示来自数据库的所有字段(姓氏)。在函数 outerRepeater_ItemDataBound
中,无法识别 DataSource
。我不知道我需要在哪里 select 特定名称的所有姓氏。
我的 repeater.ascx 中有这个:
<form id="form1" runat="server">
<div>
<asp:Repeater runat="server" ID="outerRepeater" OnItemDataBound="outerRepeater_ItemDataBound">
<HeaderTemplate>
<table>
<td><th>Name:</th></td>
<td><th>Surname:</th></td>
</table>
</HeaderTemplate>
<ItemTemplate>
<h3><%#DataBinder.Eval(Container.DataItem,"Name")%></h3>
<asp:Repeater runat="server" ID="innerRepeater" >
<ItemTemplate>
<%#Eval("Surname") %>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</div>
</form>
这在 repeater.ascx.cs
public partial class Repeater : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
outerRepeater.DataSource = GetDataSource();
outerRepeater.DataBind();
}
private DataTable GetDataSource()
{
SqlConnection myConnection = new SqlConnection(@"my connection string");
SqlDataAdapter myCommand = new SqlDataAdapter("SELECT Distinct Name FROM Person ORDER BY Name ", myConnection);
DataTable dt = new DataTable();
myCommand.Fill(dt);
myConnection.Close();
return dt;
}
private void Page_Init(object sender, EventArgs e)
{
InitializeComponent();
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
protected void outerRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
Repeater innerRepeater = e.Item.FindControl("innerRepeater") as Repeater;
DataTable dt = GetSurname();
innerRepeater.DataSource = dt;//here isn't su
innerRepeater.DataBind();
}
}
private DataTable GetSurname()
{
SqlConnection myConnection = new SqlConnection(@"my connection string");
SqlDataAdapter da = new SqlDataAdapter("Select Surname from Person ", myConnection);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
1。手动添加“Page_Load”处理程序
我不确定您是否真的需要在代码隐藏中设置 Page_Load
事件处理程序。使用默认页面设置,它将自动连接。
仅当您将 PagesSection.AutoEventWireup 设置为 false 时才需要手动设置。
2。 Table 带中继器
可以创建带有转发器的 table - How to create a three column table in ASP.Net Repeater,但需要使用正确设置的 table 标签进行稍微不同的标记:
<asp:Repeater runat="server" ID="outerRepeater" OnItemDataBound="outerRepeater_ItemDataBound">
<HeaderTemplate>
<table>
<thead>
<th>Name:</th>
<th>Surname:</th>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<h3><%#DataBinder.Eval(Container.DataItem,"Name")%></h3>
</td>
<td>
<asp:Repeater runat="server" ID="innerRepeater" >
<ItemTemplate>
<%#Eval("Surname") %>
</ItemTemplate>
</asp:Repeater>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
而且,也许 GridView 是手动 table 创建的更好选择。
3。获取给定名称的所有姓氏
您显然会得到 所有 个姓氏,因为 Select Surname from People
不会过滤任何内容 - 它只会选择所有项目。要实现您想要的效果,您必须添加一些 WHERE clause,按与当前行关联的名称进行过滤。 Name
可以通过 outerRepeater_ItemDataBound
处理程序中的 e.Item.DataItem
获得。
protected void outerRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
Repeater innerRepeater = e.Item.FindControl("innerRepeater") as Repeater;
String name = ((DataRowView)e.Item.DataItem)[0].ToString();
DataTable dt = GetSurname(name);
innerRepeater.DataSource = dt;//here isn't su
innerRepeater.DataBind();
}
}
private DataTable GetSurname(String name)
{
using (SqlConnection myConnection = new SqlConnection(@"Data Source=(LocalDb)\v11.0; Database = TSQL2012"))
{
using (SqlDataAdapter da = new SqlDataAdapter("Select Surname from Person WHERE Name = @name", myConnection))
{
da.SelectCommand.Parameters.AddWithValue("@name", name);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
}
}
4。不总是处理连接
当您在连接上调用 Close
方法时,最好使用 using
clause as it used in the previous code samples, because using
will guarantee that whatever happens the connection will be closed for sure. The same applies as well to the SqlDataAdapter
, but whether it is actually required 或只是一个好的经验法则,我不知道。
5。多个 SQL 请求
多个 SQL 请求,尤其是当所有必需信息都直接相关时,可能会导致性能非常差。您可以获取所有名称和姓氏以在页面中处理它们,或者您可以应用 Optimal way to concatenate/aggregate strings 获取包含所有需要数据的单个数据table:
aspx:
<asp:Repeater runat="server" ID="outerRepeater">
...
<ItemTemplate>
<tr >
<td>
<h3><%#DataBinder.Eval(Container.DataItem,"Name")%></h3>
</td>
<td>
<label><%#DataBinder.Eval(Container.DataItem,"ConcatenatedSurnames")%></label>
</td>
</tr>
</ItemTemplate>
代码隐藏:
private const String SelectCommand = @"
WITH Partitioned AS
(
SELECT Name,
Surname,
ROW_NUMBER() OVER (PARTITION BY Name ORDER BY Name) AS NameNumber,
COUNT(*) OVER (PARTITION BY Name ORDER BY Name) AS NameCount
FROM Person
),
Concatenated AS
(
SELECT Name,
CAST(Surname AS nvarchar) AS ConcatenatedSurnames,
NameNumber,
NameCount
FROM Partitioned
WHERE NameNumber = 1
UNION ALL
SELECT
P.Name,
CAST(C.ConcatenatedSurnames + ', ' + P.Surname AS nvarchar),
P.NameNumber,
P.NameCount
FROM Partitioned AS P
INNER JOIN Concatenated AS C
ON P.Name = C.Name AND
P.NameNumber = C.NameNumber + 1
)
SELECT
Name,
ConcatenatedSurnames
FROM Concatenated
WHERE NameNumber = NameCount";
private DataTable GetDataSource()
{
using (SqlConnection myConnection = new SqlConnection(@"Data Source=(LocalDb)\v11.0; Database = TSQL2012"))
{
using (SqlDataAdapter myCommand = new SqlDataAdapter(SelectCommand, myConnection))
{
DataTable dt = new DataTable();
myCommand.Fill(dt);
return dt;
}
}
}
我在另一个中继器中有一个中继器。如何在内部转发器中显示来自数据库的所有字段(姓氏)。在函数 outerRepeater_ItemDataBound
中,无法识别 DataSource
。我不知道我需要在哪里 select 特定名称的所有姓氏。
我的 repeater.ascx 中有这个:
<form id="form1" runat="server">
<div>
<asp:Repeater runat="server" ID="outerRepeater" OnItemDataBound="outerRepeater_ItemDataBound">
<HeaderTemplate>
<table>
<td><th>Name:</th></td>
<td><th>Surname:</th></td>
</table>
</HeaderTemplate>
<ItemTemplate>
<h3><%#DataBinder.Eval(Container.DataItem,"Name")%></h3>
<asp:Repeater runat="server" ID="innerRepeater" >
<ItemTemplate>
<%#Eval("Surname") %>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</div>
</form>
这在 repeater.ascx.cs
public partial class Repeater : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
outerRepeater.DataSource = GetDataSource();
outerRepeater.DataBind();
}
private DataTable GetDataSource()
{
SqlConnection myConnection = new SqlConnection(@"my connection string");
SqlDataAdapter myCommand = new SqlDataAdapter("SELECT Distinct Name FROM Person ORDER BY Name ", myConnection);
DataTable dt = new DataTable();
myCommand.Fill(dt);
myConnection.Close();
return dt;
}
private void Page_Init(object sender, EventArgs e)
{
InitializeComponent();
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
protected void outerRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
Repeater innerRepeater = e.Item.FindControl("innerRepeater") as Repeater;
DataTable dt = GetSurname();
innerRepeater.DataSource = dt;//here isn't su
innerRepeater.DataBind();
}
}
private DataTable GetSurname()
{
SqlConnection myConnection = new SqlConnection(@"my connection string");
SqlDataAdapter da = new SqlDataAdapter("Select Surname from Person ", myConnection);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
1。手动添加“Page_Load”处理程序
我不确定您是否真的需要在代码隐藏中设置 Page_Load
事件处理程序。使用默认页面设置,它将自动连接。
仅当您将 PagesSection.AutoEventWireup 设置为 false 时才需要手动设置。
2。 Table 带中继器
可以创建带有转发器的 table - How to create a three column table in ASP.Net Repeater,但需要使用正确设置的 table 标签进行稍微不同的标记:
<asp:Repeater runat="server" ID="outerRepeater" OnItemDataBound="outerRepeater_ItemDataBound">
<HeaderTemplate>
<table>
<thead>
<th>Name:</th>
<th>Surname:</th>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<h3><%#DataBinder.Eval(Container.DataItem,"Name")%></h3>
</td>
<td>
<asp:Repeater runat="server" ID="innerRepeater" >
<ItemTemplate>
<%#Eval("Surname") %>
</ItemTemplate>
</asp:Repeater>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
而且,也许 GridView 是手动 table 创建的更好选择。
3。获取给定名称的所有姓氏
您显然会得到 所有 个姓氏,因为 Select Surname from People
不会过滤任何内容 - 它只会选择所有项目。要实现您想要的效果,您必须添加一些 WHERE clause,按与当前行关联的名称进行过滤。 Name
可以通过 outerRepeater_ItemDataBound
处理程序中的 e.Item.DataItem
获得。
protected void outerRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
Repeater innerRepeater = e.Item.FindControl("innerRepeater") as Repeater;
String name = ((DataRowView)e.Item.DataItem)[0].ToString();
DataTable dt = GetSurname(name);
innerRepeater.DataSource = dt;//here isn't su
innerRepeater.DataBind();
}
}
private DataTable GetSurname(String name)
{
using (SqlConnection myConnection = new SqlConnection(@"Data Source=(LocalDb)\v11.0; Database = TSQL2012"))
{
using (SqlDataAdapter da = new SqlDataAdapter("Select Surname from Person WHERE Name = @name", myConnection))
{
da.SelectCommand.Parameters.AddWithValue("@name", name);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
}
}
4。不总是处理连接
当您在连接上调用 Close
方法时,最好使用 using
clause as it used in the previous code samples, because using
will guarantee that whatever happens the connection will be closed for sure. The same applies as well to the SqlDataAdapter
, but whether it is actually required 或只是一个好的经验法则,我不知道。
5。多个 SQL 请求
多个 SQL 请求,尤其是当所有必需信息都直接相关时,可能会导致性能非常差。您可以获取所有名称和姓氏以在页面中处理它们,或者您可以应用 Optimal way to concatenate/aggregate strings 获取包含所有需要数据的单个数据table:
aspx:
<asp:Repeater runat="server" ID="outerRepeater">
...
<ItemTemplate>
<tr >
<td>
<h3><%#DataBinder.Eval(Container.DataItem,"Name")%></h3>
</td>
<td>
<label><%#DataBinder.Eval(Container.DataItem,"ConcatenatedSurnames")%></label>
</td>
</tr>
</ItemTemplate>
代码隐藏:
private const String SelectCommand = @"
WITH Partitioned AS
(
SELECT Name,
Surname,
ROW_NUMBER() OVER (PARTITION BY Name ORDER BY Name) AS NameNumber,
COUNT(*) OVER (PARTITION BY Name ORDER BY Name) AS NameCount
FROM Person
),
Concatenated AS
(
SELECT Name,
CAST(Surname AS nvarchar) AS ConcatenatedSurnames,
NameNumber,
NameCount
FROM Partitioned
WHERE NameNumber = 1
UNION ALL
SELECT
P.Name,
CAST(C.ConcatenatedSurnames + ', ' + P.Surname AS nvarchar),
P.NameNumber,
P.NameCount
FROM Partitioned AS P
INNER JOIN Concatenated AS C
ON P.Name = C.Name AND
P.NameNumber = C.NameNumber + 1
)
SELECT
Name,
ConcatenatedSurnames
FROM Concatenated
WHERE NameNumber = NameCount";
private DataTable GetDataSource()
{
using (SqlConnection myConnection = new SqlConnection(@"Data Source=(LocalDb)\v11.0; Database = TSQL2012"))
{
using (SqlDataAdapter myCommand = new SqlDataAdapter(SelectCommand, myConnection))
{
DataTable dt = new DataTable();
myCommand.Fill(dt);
return dt;
}
}
}