loop of repetition C # asp components

3

Would you like to make a loop in this case?

PaperBLL paperBLL = new PaperBLL();
List<PaperEO> papers = paperBLL.SelectPapers();
HyperLink1.ImageUrl = "~/App_Themes/MultiMercadoLibertyExclusiva/Capas/" + papers.First(x => x.id == 1).capa;
HyperLink2.ImageUrl = "~/App_Themes/MultiMercadoLibertyExclusiva/Capas/" + papers.First(x => x.id == 2).capa;
HyperLink3.ImageUrl = "~/App_Themes/MultiMercadoLibertyExclusiva/Capas/" + papers.First(x => x.id == 3).capa;
HyperLink4.ImageUrl = "~/App_Themes/MultiMercadoLibertyExclusiva/Capas/" + papers.First(x => x.id == 4).capa;
HyperLink5.ImageUrl = "~/App_Themes/MultiMercadoLibertyExclusiva/Capas/" + papers.First(x => x.id == 5).capa;
HyperLink6.ImageUrl = "~/App_Themes/MultiMercadoLibertyExclusiva/Capas/" + papers.First(x => x.id == 6).capa;
HyperLink7.ImageUrl = "~/App_Themes/MultiMercadoLibertyExclusiva/Capas/" + papers.First(x => x.id == 7).capa;
HyperLink8.ImageUrl = "~/App_Themes/MultiMercadoLibertyExclusiva/Capas/" + papers.First(x => x.id == 8).capa;
HyperLink9.ImageUrl = "~/App_Themes/MultiMercadoLibertyExclusiva/Capas/" + papers.First(x => x.id == 9).capa;
HyperLink10.ImageUrl = "~/App_Themes/MultiMercadoLibertyExclusiva/Capas/" + papers.First(x => x.id == 10).capa;
HyperLink11.ImageUrl = "~/App_Themes/MultiMercadoLibertyExclusiva/Capas/" + papers.First(x => x.id == 11).capa;
HyperLink12.ImageUrl = "~/App_Themes/MultiMercadoLibertyExclusiva/Capas/" + papers.First(x => x.id == 12).capa;

(...)

<ul>
    <li><asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/App_Themes/MultiMercadoLibertyExclusiva/Capas/01.png" rel="Shadowbox[Vocation]"><asp:Image ID="Image2" runat="server" Width="131" ImageUrl="~/App_Themes/MultiMercadoLibertyExclusiva/Capas/01.png" /></asp:HyperLink></li>
    <li><asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/App_Themes/MultiMercadoLibertyExclusiva/Capas/02.png" rel="Shadowbox[Vocation]"><asp:Image ID="Image3" runat="server" Width="131" ImageUrl="~/App_Themes/MultiMercadoLibertyExclusiva/Capas/02.png" /></asp:HyperLink></li>
    <li><asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl="~/App_Themes/MultiMercadoLibertyExclusiva/Capas/03.png" rel="Shadowbox[Vocation]"><asp:Image ID="Image4" runat="server" Width="131" ImageUrl="~/App_Themes/MultiMercadoLibertyExclusiva/Capas/03.png" /></asp:HyperLink></li>
    <li><asp:HyperLink ID="HyperLink4" runat="server" NavigateUrl="~/App_Themes/MultiMercadoLibertyExclusiva/Capas/04.png" rel="Shadowbox[Vocation]"><asp:Image ID="Image5" runat="server" Width="131" ImageUrl="~/App_Themes/MultiMercadoLibertyExclusiva/Capas/04.png" /></asp:HyperLink></li>
    <li><asp:HyperLink ID="HyperLink5" runat="server" NavigateUrl="~/App_Themes/MultiMercadoLibertyExclusiva/Capas/05.png" rel="Shadowbox[Vocation]"><asp:Image ID="Image6" runat="server" Width="131" ImageUrl="~/App_Themes/MultiMercadoLibertyExclusiva/Capas/05.png" /></asp:HyperLink></li>
    <li><asp:HyperLink ID="HyperLink6" runat="server" NavigateUrl="~/App_Themes/MultiMercadoLibertyExclusiva/Capas/06.png" rel="Shadowbox[Vocation]"><asp:Image ID="Image7" runat="server" Width="131" ImageUrl="~/App_Themes/MultiMercadoLibertyExclusiva/Capas/06.png" /></asp:HyperLink></li>
    <li><asp:HyperLink ID="HyperLink7" runat="server" NavigateUrl="~/App_Themes/MultiMercadoLibertyExclusiva/Capas/07.png" rel="Shadowbox[Vocation]"><asp:Image ID="Image8" runat="server" Width="131" ImageUrl="~/App_Themes/MultiMercadoLibertyExclusiva/Capas/07.png" /></asp:HyperLink></li>
    <li><asp:HyperLink ID="HyperLink8" runat="server" NavigateUrl="~/App_Themes/MultiMercadoLibertyExclusiva/Capas/08.png" rel="Shadowbox[Vocation]"><asp:Image ID="Image9" runat="server" Width="131" ImageUrl="~/App_Themes/MultiMercadoLibertyExclusiva/Capas/08.png" /></asp:HyperLink></li>
    <li><asp:HyperLink ID="HyperLink9" runat="server" NavigateUrl="~/App_Themes/MultiMercadoLibertyExclusiva/Capas/09.png" rel="Shadowbox[Vocation]"><asp:Image ID="Image10" runat="server" Width="131" ImageUrl="~/App_Themes/MultiMercadoLibertyExclusiva/Capas/09.png" /></asp:HyperLink></li>
    <li><asp:HyperLink ID="HyperLink10" runat="server" NavigateUrl="~/App_Themes/MultiMercadoLibertyExclusiva/Capas/10.png" rel="Shadowbox[Vocation]"><asp:Image ID="Image11" runat="server" Width="131" ImageUrl="~/App_Themes/MultiMercadoLibertyExclusiva/Capas/10.png" /></asp:HyperLink></li>
</ul>
    
asked by anonymous 02.07.2014 / 19:17

2 answers

3

It may be a situation to resolve with a control Repeater ". You can set any classes that implement IEnumerable or IListSource as data source .

During the ItemDataBound event you will have the collection member object and all the controls in the ItemTemplate available.

Page.aspx

<form id="form1" runat="server">
    <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
        <ItemTemplate>
            <asp:HyperLink ID="meuHyperLink" runat="server">HyperLink</asp:HyperLink>
        </ItemTemplate>
    </asp:Repeater>
</form>

Page.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {
        var minhaColecao = new List<string>(); //Assuma como sendo sua colecao.

        Repeater1.DataSource = minhaColecao;
        Repeater1.DataBind();
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        var item = (string) e.Item.DataItem; // No seu caso, substitua o string cast por PaperEO.
        var controle = (HyperLink)e.Item.FindControl("meuHyperLink");
        controle.ImageUrl = "~/App_Themes/MultiMercadoLibertyExclusiva/Capas/" + item;
    }
    
02.07.2014 / 21:27
1

Use a generic function:

private IEnumerable<T> GetControlList<T>(ControlCollection controlCollection)
where T : Control
{
    foreach (varcontrol in controlCollection)
    {
        if (control is T)
            yield return (T)control;
    }
}

Usage:

foreach (var link in GetControlList<HyperLink>(Page.Controls))
{
    // Coloque a lógica aqui.
}
    
02.07.2014 / 19:33