嵌套转发器不会显示列表中的绑定信息

时间:2016-01-12 11:18:02

标签: c# asp.net

我正在尝试使用转发器来获取显示行布局信息的页面。我有一个工作,允许我动态创建超链接,但我不能让我的嵌套转发器工作,以显示文件的创建日期。是否有可能使用转发器动态显示列表中的多个变量,正如我在下面尝试的那样?

的.aspx

<asp:Repeater id="repLinks" runat="server">
   <ItemTemplate>
       <tr><td>

      <asp:HyperLink runat="server" NavigateUrl='<%# Container.DataItem.ToString() %>' Text="<%# Container.DataItem.ToString().Split('\\').Last() %>"  />    

            <td>  

               <asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="Repeater2_ItemDataBound" >
                  <ItemTemplate>
                   <%# Container.DataItem.ToString()%>  
                     </ItemTemplate> 
               </asp:Repeater>             
           </td>

           <td>
               Submitted By <!--add repeater-->
           </td>
           <td>
               Mark as Billed <!--add repeater-->
           </td>
           </td></tr>
            </ItemTemplate>
           </asp:Repeater>

.aspx.cs

public List<string> CD = new List<string>();

    protected void Page_Load(object sender, EventArgs e)
    {
        //Welcomes User
        string Uname = Environment.UserName;
        UserName.Font.Size = 17;
        UserName.Text = "Welcome: " + Uname;

        //gives path and constructs lists for directory paths and file links
        string root = "C:\\Users\\James\\Documents\\Visual Studio 2015\\WebSites";
        List<string> lLinks = new List<string>();

        //adds files to list
        foreach (var path in Directory.GetDirectories(@root))
        {
            foreach (var path2 in Directory.GetFiles(path))
            {
                lLinks.Add(path2);

                CD.Add(File.GetCreationTime(path2).Date.ToString("yyyy-mm-dd"));
            }
        }

        //Define your list contents here 
        repLinks.DataSource = lLinks;
        repLinks.DataBind();
    }

    protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater Repeater2 = (Repeater)(e.Item.FindControl("Repeater2"));
            Repeater2.DataSource = CD;
            Repeater2.DataBind();
        }
    }

1 个答案:

答案 0 :(得分:0)

您的代码的问题是您在ItemDataBound转发器本身的Repeater2事件中绑定嵌套转发器控件(Repeater2),因为ItemDataBound事件被触发,所以它永远不会被触发对于集合中的每个项目,当它与转发器控件绑定时。

你应该在你的父转发器的ItemDataBound事件中写下这样的逻辑: -

<asp:Repeater id="repLinks" runat="server" OnItemDataBound="Repeater1_ItemDataBound">

然后,在此事件处理程序中编写逻辑: -

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
   if (e.Item.ItemType == ListItemType.Item || 
                                      e.Item.ItemType == ListItemType.AlternatingItem)
   {
        Repeater Repeater2 = (Repeater)(e.Item.FindControl("Repeater2"));
        Repeater2.DataSource = CD;
        Repeater2.DataBind();
   }
}

此外,在Page_Load事件中,您应该仅在初始页面加载时绑定父转发器Repeater1,然后将其包装在!IsPostBack内并填充数据源lLinks&amp; CD来自在Page_Load事件中执行此操作的单独方法。