Where do you add the controls? to a place holder? I have given below a rough idea of how you could do this:
Add a place holder that will have the labels:
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="plcMain" runat="server">
</asp:PlaceHolder>
<asp:Label runat="server" ID="lblInfo"></asp:Label>
</div>
</form>
Code - behind:
protected void Page_Load(object sender, EventArgs e)
{
AddLinks();
}
private void AddLinks()
{
for (int i=0;i<10;i++)
{
var linkButton = new LinkButton
{
ID = "lnkBtn" + i.ToString(),
CommandArgument = i.ToString(),
Text = "Link " + i.ToString()
};
linkButton.Click += new EventHandler(linkButton_Click);
plcMain.Controls.Add(linkButton);
}
}
protected void linkButton_Click(object sender, EventArgs e)
{
var btn = sender as LinkButton;
lblInfo.Text = "Link's command argument = " + btn.CommandArgument;
}
If you notice, you don't even need the FindControl method, just the
linkButton_Click
event handler. If you insist on using the FindControl method, you can do something like
LinkButton control = plcMain.FindControl("lnkBtn1") as LinkButton;
In your case, you use the
FindControl
method on the page. Try a recursive
FindControl
if you don't wish to start the search with a certain control
http://weblogs.asp.net/palermo4/archive/2007/04/13/recursive-findcontrol-t.aspx[
^]