我有以下转发器:
<asp:Repeater ID="repeater1" runat="server" OnItemDataBound="repeater1_ItemDataBound">
<HeaderTemplate>
...
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label runat="server" Text='<%#Eval("id")%>'> </asp:Label></td>
<td>
<asp:Label runat="server" Text='<%#Eval("ssdvalue")%>'></asp:Label>
</td>
<td>
<asp:Repeater runat="server" ID="repeater2">
<ItemTemplate>
<asp:Label Text='<%#Eval("stptime")%>' runat="server"></asp:Label>
</ItemTemplate>
</asp:Repeater>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
现在在上面的代码中有两个中继器:repeater1和repeater2(在标签内)
上面我写了以下代码:
DataSet ds = new DataSet();
ds = cls.ReturnDataSet("sp_data",
new SqlParameter("@From_Time", "2014-11-01 00:00:00.000"),
new SqlParameter("@To_Time", "2014-11-03 00:00:00.000"));
repeater1 .DataSource = ds.Tables[0];
repeater1 .DataBind();
这将绑定repeater1。
对于Repeater2,我写了以下代码:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataSet ds = new DataSet();
ds = cls.ReturnDataSet("sp_data",
new SqlParameter("@From_Time", "2014-11-01 00:00:00.000"),
new SqlParameter("@To_Time", "2014-11-03 00:00:00.000"));
Repeater rpt = (Repeater)e.Item.FindControl("Repeater2");
rpt.DataSource = ds.Tables[1];
rpt.DataBind();
}
}
现在整个输出就像:
Id Ssdvalue stptime
1 112 3 hours 4hours 5 hours 6 hours 12minutes 20minutes
2 234 3 hours 4hours 5 hours 6 hours 12minutes 20minutes
3 450 3 hours 4hours 5 hours 6 hours 12minutes 20minutes
4 987 3 hours 4hours 5 hours 6 hours 12minutes 20minutes
5 456 3 hours 4hours 5 hours 6 hours 12minutes 20minutes
6 091 3 hours 4hours 5 hours 6 hours 12minutes 20minutes
但是上面的输出不正确。
I want output like Below:
Id Ssdvalue stptime
1 112 3 hours
2 234 4hours
3 450 5 hours
4 987 6 hours
5 456 12minutes
6 091 20minutes
那么我怎样才能获得上述输出?
答案 0 :(得分:2)
这样做: -
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
DataSet ds = new DataSet();
ds = cls.ReturnDataSet("sp_data",
new SqlParameter("@From_Time", "2014-11-01 00:00:00.000"),
new SqlParameter("@To_Time", "2014-11-03 00:00:00.000"));
Label Label1 = (Label)e.Item.FindControl("Label1");
Repeater rpt = (Repeater)e.Item.FindControl("Repeater2");
var source = ds.Tables[1].AsEnumerable()
.Where(x => x.Field<int>("Id") == Convert.ToInt32(Label1.Text))
.Select(x => new
{
stptime= x.Field<DateTime>("stptime")
});
rpt.DataSource = source ;
rpt.DataBind();
}
}
由于您需要使用相同的ID进行映射,因此您必须根据ID过滤数据,即Id
1将映射到stpTime
3小时,依此类推。因此,我们需要使用LINQ Where
子句过滤我正在进行的主数据源,并且我们从Label
获取相应的Id。