目前我的页面上有一个家长中继器和两个孩子中继器。我想从我们的数据库中动态拉取数据,并在中继器中显示它们。我遵循了微软的指南和一些关于如何做到这一点的SO问题,这是我得出的结论:
<!-- start parent repeater -->
<asp:repeater id="parentRepeater" runat="server">
<itemtemplate>
<li><div id="et_<%# DataBinder.Eval(Container.DataItem,"EquipmentTypeName") %>"><%# DataBinder.Eval(Container.DataItem,"EquipmentTypeName") %><label><input type="checkbox"></label></div><ul runat="server">
<!-- start child repeater1 -->
<asp:repeater id="Repeater1" datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("myrelation") %>' runat="server">
<itemtemplate>
<li><div id="et_<%# DataBinder.Eval(Container.DataItem, "[\"MakeID\"]")%>"><%# DataBinder.Eval(Container.DataItem, "[\"MakeID\"]")%><label><input type="checkbox"></label></div><ul runat="server">
<!-- start child repeater2 -->
<asp:repeater id="childRepeater2" datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("myrelation2") %>' runat="server">
<itemtemplate>
<li><div id="et_<%# DataBinder.Eval(Container.DataItem, "[\"YearID\"]")%>"><%# DataBinder.Eval(Container.DataItem, "[\"YearID\"]")%><label><input type="checkbox"></label></div><ul runat="server">
</ul></li></itemtemplate>
</asp:repeater>
<!-- end child repeater2 -->
</ul></li></itemtemplate>
</asp:repeater>
<!-- end child repeater1 -->
</ul></li></itemtemplate>
</asp:repeater>
<!-- end parent repeater -->后面的代码如下:
protected void Page_Load(object sender, EventArgs e)
{
//Create the connection and DataAdapter for the Authors table.
string Connection = WebConfigurationManager.ConnectionStrings["md_dbConnectionString"].ConnectionString;
SqlConnection cnn = new SqlConnection(Connection);
SqlDataAdapter cmd1 = new SqlDataAdapter("select * from EquipmentType", cnn);
//Create and fill the DataSet.
DataSet ds = new DataSet();
cmd1.Fill(ds, "EquipmentType");
SqlDataAdapter cmd2 = new SqlDataAdapter("select * from Make", cnn);
cmd2.Fill(ds, "Make");
//Create a second DataAdapter for the Titles table.
SqlDataAdapter cmd3 = new SqlDataAdapter("select distinct MakeID, TypeID, YearID from Parts", cnn);
cmd3.Fill(ds, "Parts");
//Create the relation bewtween the Authors and Titles tables.
ds.Relations.Add("myrelation",
ds.Tables["EquipmentType"].Columns["EquipmentTypeID"],
ds.Tables["Parts"].Columns["TypeID"]);
ds.Relations.Add("myrelation2",
ds.Tables["Make"].Columns["MakeID"],
ds.Tables["Parts"].Columns["MakeID"]);
//Bind the Authors table to the parent Repeater control, and call DataBind.
parentRepeater.DataSource = ds;
Page.DataBind();
//Close the connection.
cnn.Close();
}如果我加载只有一个子中继器的页面,一切工作正常,我得到了正确的数据显示,但当我添加第二个子中继器时,我得到了以下错误消息:Unable to cast object of type 'System.Data.DataRow' to type 'System.Data.DataRowView'. This fires on the数据源=‘<%#ASP%>’`ASP行。
我尝试从中提取的表的结构是这样的:
EquipmentType
EquipmentTypeID | EquipmentTypeName
制作
MakeID | MakeName
年
YearID
零件
PartID | MakeID | YearID | TypeID
每个表都引用零件表。
或多或少我想让转发器做的是显示所有的EquipmentTypes,然后只显示制造商,因为我们在数据库中有它的一个部件。一旦选择了制造商,则只显示数据库中该制造商和设备类型的部件年份。
发布于 2016-03-17 05:21:56
你有没有试着把它转换成DataRow:
<%# ((DataRow)Container.DataItem).GetChildRows("myrelation2") %>发布于 2021-03-27 09:25:16
对于任何其他遇到此问题的人。可能是页面顶部缺少一个标记。<%@导入Namespace="System.Data“%>
https://stackoverflow.com/questions/36044736
复制相似问题