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>
You have to put
lb
control which I assume isLabel
into yourRepeater1
control then make an event on you repeater controlOnItemDataBound
and put your code there exclude this:Edit: Your .aspx should seems like this:
Hope this helps :)
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: