2

On the loading of a page, I want to access a list of names via an interface and create link buttons for each name in that list. Also clicking any of the link buttons leads to another page.

I'm new to ASP.net so I'm not sure where the link buttons should be created. At first I thought to create it in the .aspx file but how does the repeater know to create as many buttons in the list so then I did it in the page load function and bind the names to the button. But this doesn't work:

 public void Repeater1_ItemDataBound(object sender, EventArgs e)
    {
        LinkButton lb = (LinkButton)this.FindControl("lb");

        IComparisonDataService ds = new ComparisonDataService();
        IList<string> apps = ds.GetApplicationList();
        foreach (var app in apps)
            lb.Text = app;
    }

And for the .aspx I just have a repeater object with link button:

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
  <link href="Styles/Layout.css" rel="stylesheet" type="text/css" />
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
  <div align="center" class="submitButton">
    <asp:Repeater ID="Repeater1" runat="server"  OnItemDataBound="Repeater1_ItemDataBound">
        <ItemTemplate>
            <asp:LinkButton ID="lb" runat="server" />
        </ItemTemplate>
    </asp:Repeater>
       </div>

charlie123
  • 253
  • 1
  • 5
  • 17
  • Ok I edited my answer with correct syntax of using Repeater ItemDataBound event :) sorry for my mistakes – Harry89pl Aug 03 '12 at 10:48
  • thanks for answering but it now gives an error of ['ASP.dashboard_aspx' does not contain a definition for 'Repeater1_ItemDataBound' and no extension method 'Repeater1_ItemDataBound' accepting a first argument of type 'ASP.dashboard_aspx' could be found] Does the code work for you? – charlie123 Aug 03 '12 at 11:12
  • yes I tested it and it's working do you have copy my `Repeater1_OnItemDataBound` method? – Harry89pl Aug 03 '12 at 13:40
  • yes I did, but it was looking for a method called Repeater1_ItemDataBound so I renamed it to that. Thank you for all your help! – charlie123 Aug 06 '12 at 08:27

2 Answers2

1

You have to put lb control which I assume is Label into your Repeater1 control then make an event on you repeater control OnItemDataBound and put your code there exclude this:

protected void Page_Load(object sender, EventArgs e)
    {
        List<string> str = new List<string>{"I", "You", "They"};
        Repeater1.DataSource = str;
        Repeater1.DataBind();
    }

    protected void Repeater1_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            LinkButton lb = (LinkButton)e.Item.FindControl("lb");
            string str = (string) e.Item.DataItem;
            lb.Text = str;
        }

    }

Edit: Your .aspx should seems like this:

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
     <%-- here you can also add some <HeaderTemplate> if you need a headers --%>
     <ItemTemplate>
         <%-- here you can put your controls --%>
         <asp:LinkButton ID="lb" runat="server"/>
     </ItemTemplate>
</asp:Repeater>

Hope this helps :)

Harry89pl
  • 2,415
  • 12
  • 42
  • 63
  • Sorry but how/where do you define a Repeater control. Is that in the .aspx file. Also lb was meant to be link button. So if I change the object to link button it should still work, right? Then in the page load function do i just call Repeater1_ItemDataBound() – charlie123 Aug 03 '12 at 09:25
  • I'm edited my answer and changed a little code for your purpose – Harry89pl Aug 03 '12 at 09:34
  • thanks! but i'm still unsure how to add the control to the repeater and linkbutton. – charlie123 Aug 03 '12 at 09:42
  • pls edit your question and provide your code of .aspx page with this ` – Harry89pl Aug 03 '12 at 09:45
  • thanks again! I did this but when I do sender.FindControl, it states object does not contain a defintion for 'Find Control' – charlie123 Aug 03 '12 at 09:57
  • try now but your `LinkButton` have to get same ID name that you trying to find in page in my case `lb` in yours `LinkButton1` – Harry89pl Aug 03 '12 at 10:00
  • I tried that and changed the ID to fit my case but nothing is displayed anymore on the site. No repeater or link button can be seen in the design view either. Is that meant to be the case? – charlie123 Aug 03 '12 at 10:13
1

Don't databind to a list of controls, databind to the actual data, in this case your list of strings stored in the variable apps. Then add a databinding expression to the template to set the properties of the LinkButton control:

<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Height="200px" Width="150px" BackColor="#33CC33"
    BorderColor="Black" Font-Underline="False" Text='<%# Container.DataItem #>'></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
Jonas Høgh
  • 10,358
  • 1
  • 26
  • 46