我试图让页面使用中继器在行布局中显示信息。 我有一个工作,允许我动态创建超链接,但我不能让我的嵌套转发器工作,以显示文件的创建日期。 是否有可能使用中继器动态显示列表中的多个变量,因为我正在试图在下面做?
的.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(); } }I am trying to get a page to display information in a row layout using repeaters. I have one working that allows me to dynamically create hyperlinks, however i cant get my nested repeater to work to display the date the file was created. Is it possible to use repeaters to dynamically display multiple variables from lists as i'm trying to do below?
.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(); } }最满意答案
你的代码的问题在于你绑定Repeater2 repeater本身的ItemDataBound事件中的嵌套Repeater控件(Repeater2),它永远不会被触发,因为当它被绑定到Repeater控件时,它会为集合中的每个项目触发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事件中,您应该在初始页面加载时绑定Parent Repeater Repeater1 ,以便将其包裹在里面!IsPostBack并从Page_Load事件中以单独的方法填充您的数据源lLinks & CD 。
The issue with your code is that you are binding the nested repeater control (Repeater2) in the ItemDataBound event of Repeater2 repeater itself which will never get fired because ItemDataBound event is fired for each item in collection when it is bounded to the repeater control.
You should write the logic in ItemDataBound event of your parent repeater like this:-
<asp:Repeater id="repLinks" runat="server" OnItemDataBound="Repeater1_ItemDataBound">Then, write the logic in this event handler:-
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(); } }Also, In the Page_Load event you should bind the Parent Repeater Repeater1 on just the initial page load so wrap it inside !IsPostBack and populate your datasource lLinks & CD from a separate method intead of doing it in Page_Load event.
更多推荐
发布评论