C#嵌套转发器不工作

时间:2017-12-13 19:07:44

标签: c# asp.net

我一直在寻找这个3天所以请,任何反馈将不胜感激。 我有一个父转发器,可以提取预期的数据,但我正在尝试嵌套子转发器,查找控件并填充数据。但到目前为止还没有奏效。由于某些原因,下面的嵌套转发器未被拾取以显示正确的数据。 有什么建议吗?

Asp.Net代码在这里:

<asp:Repeater ID="EquipmentRepeater" runat="server" OnItemDataBound="Repeater2_ItemDataBound" > 
           <ItemTemplate>
                 <b>Equipment:</b>
               <%# DataBinder.Eval(Container.DataItem, "Equip") %>&nbsp;
                <%# DataBinder.Eval(Container.DataItem, "Location") %>&nbsp;
               </ItemTemplate>
       </asp:Repeater>




protected void Repeater2_ItemDataBound(object sender System.Web.UI.WebControls.RepeaterItemEventArgs e)
    {
        SqlConnection con = new SqlConnection(ConString);
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater Repeater2 = (Repeater)e.Item.FindControl("EquipmentRepeater");

            System.Data.DataTable ds = new System.Data.DataTable();
            SqlCommand cmd1 = new SqlCommand(" Select HourID, Equip,Location FROM Equip where HourID=@id");

            cmd1.Parameters.Add("@id", SqlDbType.Int).Value = id;
            con.Open();
            cmd1.Connection = con;
            cmd1.ExecuteReader();
            con.Close();
            SqlDataAdapter ad = new SqlDataAdapter(cmd1);
            // DataTable ds = new DataTable();
            ad.Fill(ds);
            con.Close();


            //Need to assign the Data in datatable
            Repeater2.DataSource = ds;
            Repeater2.DataBind();
        }

    }

1 个答案:

答案 0 :(得分:0)

我解决了它,有两个问题:

1- Repeater ItemDataBound需要位于父转发器Even Handler中。 2 - 第二个问题是子转发器的Id参数ID没有任何值因此我在父转发器中为Id创建了一个隐藏的字段并在ItemDataBound上调用它

<asp:HiddenField ID="ID" runat="server" Value='<%# Eval("ID") %>' />

 protected void Repeater2_ItemDataBound(object sender System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
    SqlConnection con = new SqlConnection(ConString);
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Repeater Repeater2 = (Repeater)e.Item.FindControl("EquipmentRepeater");

        System.Data.DataTable ds = new System.Data.DataTable();
        SqlCommand cmd1 = new SqlCommand(" Select HourID, Equip,Location FROM Equip where HourID=@id");

          HiddenField id = (HiddenField)e.Item.FindControl("ID");
            int parsedId = int.Parse(id.Value);
            cmd1.Parameters.Add("@id", SqlDbType.Int).Value = parsedId;
        con.Open();
        cmd1.Connection = con;
        cmd1.ExecuteReader();
        con.Close();
        SqlDataAdapter ad = new SqlDataAdapter(cmd1);
        // DataTable ds = new DataTable();
        ad.Fill(ds);
        con.Close();


        //Need to assign the Data in datatable
        Repeater2.DataSource = ds;
        Repeater2.DataBind();
    }

}

谢谢大家,祝你好运