使用SQL Server,我创建了一个组详细信息表&事件表。向表中添加事件时,它与特定组关联。
下面,我可以使用转发器显示所有组详细信息。但是,我还想显示与每个组相关联的事件。
以下是我的群组详细信息中继器:
<div class="panel-group" id="accordion">
<asp:Repeater ID="rptAccordian" runat="server">
<ItemTemplate>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse<%= getIndex() %>">
<%# Eval("Group_Name") %>
</a>
</h3>
</div>
<!-- Get the current index-->
<div id="collapse<%=getIndex()%>" class="panel-collapse collapse">
<div class="panel-body">
<%# Eval("Group_Desc") %>
</div>
</div>
</div>
<!--then increment it for the next repeater element-->
<% incrementIndex(); %>
</ItemTemplate>
</asp:Repeater>
</div>
代码背后:
cmd.CommandText = "select * from Groups News ORDER BY Group_Id DESC";
cmd.Connection = con;
con.Open();
rptAccordian.DataSource = cmd.ExecuteReader();
rptAccordian.DataBind();
con.Close();
以下代码显示事件,但不显示每组:
<div class="panel-body" style="height: 400px; overflow-y: scroll;">
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand" EnableViewState="false">
<ItemTemplate>
<div>
<table class="table table-striped">
<tr>
<th><%#Eval("Event_Title")%></th>
<td></td>
<td></td>
</tr>
<tr>
<td>Event Group ID</td>
<td><%#Eval("Event_Cost") %></td>
<td></td>
</tr>
<tr>
<td>Event Type</td>
<td><%#Eval("Event_Type") %></td>
<td></td>
</tr>
<tr>
<td>Event ID</td>
<td><%#Eval("Id") %></td>
<td></td>
</tr>
<tr>
<td>Cost</td>
<td><%#Eval("Event_Cost") %></td>
<td></td>
</tr>
<tr>
<td>Description</td>
<td><%#Eval("Event_Desc") %></td>
<td></td>
</tr>
<tr>
<td>Organiser</td>
<td><%#Eval("Event_Organiser") %></td>
<td></td>
</tr>
<tr>
<td>Date</td>
<td><%#Eval("Event_Date") %></td>
<td></td>
</tr>
<tr>
<td>Location</td>
<td><%#Eval("Event_Location") %></td>
<td></td>
</tr>
<tr>
<td>Time</td>
<td><%#Eval("Event_Start_Time") %></td>
<td></td>
</tr>
<br />
</table>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
代码背后:
connR = new SqlConnection(connectionStringR);
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Events", connR);
DataTable dt = new DataTable();
sda.Fill(dt);
Repeater1.DataSource = dt;
Repeater1.DataBind();
这是我的桌面结构:
组表:
DDL:
CREATE TABLE [dbo].[Group_Events] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Group_Event_ID] INT NULL,
[Event_Type] NVARCHAR (50) NULL,
[Event_Title] NVARCHAR (50) NULL,
[Event_Cost] FLOAT (53) NULL,
[Event_Desc] NVARCHAR (50) NULL,
[Event_Organiser] NVARCHAR (50) NULL,
[Event_Date] DATE NULL,
[Event_Location] NVARCHAR (50) NULL,
[Event_Start_Time] DATETIME NULL,
PRIMARY KEY CLUSTERED ([Id] ASC));
CREATE TABLE [dbo].[Groups] (
[Group_Id] INT IDENTITY (1, 1) NOT NULL,
[Group_Name] NVARCHAR (50) NULL,
[Group_Desc] NVARCHAR (1000) NULL,
PRIMARY KEY CLUSTERED ([Group_Id] ASC));
Group_Id(PK)是Events表中的FK Group_Event_ID。
我不确定如何将我在上面创建的Events表嵌套到我的rptAccordian for Groups中。
我已尝试使用以下代码:
<asp:Repeater ID="rptAccordian" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse<%= getIndex() %>">
<%# Eval("Group_Name") %>
</a>
</h3>
</div>
<!-- Get the current index-->
<div id="collapse<%=getIndex()%>" class="panel-collapse collapse">
<div class="panel-body">
<p>
<%# Eval("Group_Desc") %>
</p>
<table id="tblGroupEvents" class="table table-bordered">
<tr>
<th><%# Eval("Event_Title") %></th>
<td></td>
<td></td>
</tr>
</table>
</div>
</div>
</div>
<!--then increment it for the next repeater element-->
<% incrementIndex(); %>
</ItemTemplate>
</asp:Repeater>
代码背后:
private void BindRepeater()
{
string constr = ConfigurationManager.ConnectionStrings["BallinoraDBConnectionString1"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
//ORDER BY DATE
cmd.CommandText = "select * from Groups News ORDER BY Group_Id DESC";
cmd.Connection = con;
con.Open();
rptAccordian.DataSource = cmd.ExecuteReader();
rptAccordian.DataBind();
con.Close();
}
}
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Table table = e.Item.FindControl("tblGroupEvents") as Table;
string Group_Id = DataBinder.Eval(e.Item.DataItem, "Group_Id").ToString();
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Group_Events WHERE (Group_Event_ID = '" + Group_Id + "'", con);
DataTable dt = new DataTable();
sda.Fill(dt);
table.DataBind();
}
但是我收到了这个错误:
其他信息:DataBinding:&#39; System.Data.Common.DataRecordInternal&#39;不包含名称为&#39; Event_Title&#39;。
的媒体资源
有什么想法吗?