Loop Loop C# Asp components

Asked

Viewed 133 times

3

You’d have to do a loop 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>
  • This is in a Code Behind?

  • I forgot to mention, yes you are!

  • Of curiosity, it would not fit something of the type HyperLink[12].ImageUrl in your solution?

  • @Bacco, I don’t know this "technique", I can do this?

  • @Diegozanardo this is just my question :) Maybe I can, but the lambda may not work as expected.

  • @Diegozanardo saw that you have already achieved the solution. Solved everything? Solved this part too papers.First(x => x.id == 12).capa? Or you don’t need it anymore?

Show 1 more comment

2 answers

3


Maybe it’s a situation to be solved with a control Repeater. You can set any class that implements Ienumerable or Ilistsource as data source.

During the event ItemDataBound you will have at your disposal the collection’s member object and all controls present in the Itemtemplate.

Pagina.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>

Pagina.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;
    }
  • Within <Itemtemplate>, would I have to create all Hyperlink? Like you did for "1"?

  • @Diegozanardo No - just one; note that in the Itemdatabound event the control is 'searched' via method FindControl. The Repeater control will create as many instances of control as necessary (one for each member of the collection. I will edit my reply to make the control name clearer.

  • It worked @Onosendai, saved many lines of code, hahahahah. Obg!

  • @Diegozanardo I’m glad it worked! As an additional tip, it’s worth mentioning that you can include as many controls as you want in ItemTemplate. Just remember to use the cast for the right type when using FindControl, because this method just returns Objects.

  • There’s a way I set the id to start from 1 instead of 0?

  • You say the content created automatically by Repeater? I have no way to check at this time, but I would say that it is not possible to control the initial value. I will check later for you.

Show 1 more comment

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;
    }
}

Use:

foreach (var link in GetControlList<HyperLink>(Page.Controls))
{
    // Coloque a lógica aqui.
}
  • Did not enter the foreach!

  • @Diegozanardo Which one of them?

  • foreach (var link in Getcontrollist<Hyperlink>(Page.Controls))

  • Return the value of GetControlList in a list and debug. What appears in it?

  • She came Nulla!

  • The types of your objects are HyperLink same? They appear within Page.Controls?

  • Yes. I edited the question.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.