嵌套转发器会引发转换错误

时间:2016-03-16 19:02:00

标签: c# asp.net repeater

我目前在我的页面上有一个父中继器和两个子中继器。我想从我们的数据库中动态提取数据并在转发器中显示它们。我遵循了微软指南和一些关于如何做到这一点的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 datasource =& #39;&lt;%#((DataRowView)Container.DataItem).Row.GetChildRows(&#34; myrelation2&#34;)%&gt;&#39;`ASP line。

我试图从中获取的表格是这样构建的:

EquipmentType

EquipmentTypeID | EquipmentTypeName

MakeID | MakeName

YearID

零件

PartID | MakeID | YearID | TYPEID

每个表都引用了Parts表。

或多或少我希望转发器做的是显示所有的EquipmentTypes,然后只显示make我们在数据库中有它的一部分。选择make后,只显示我们在数据库中有该品牌和设备类型的部件的年份。

2 个答案:

答案 0 :(得分:1)

您是否尝试将其投射到DataRow:

<%# ((DataRow)Container.DataItem).GetChildRows("myrelation2") %>

答案 1 :(得分:0)

对于遇到此问题的任何其他人。可能是页面顶部缺少标签。 <%@ 导入命名空间="System.Data" %>