adding linkbuttons at run time?

B

bazzer

hey,
i have an ASP.NET webform linking to a microsoft access database. at
runtime, i need the application to add x linkbuttons to a panel. x will
be the number of entries under a certain field in one of the tables
from the database, which can vary each time.

i just cant get my mind around how to go about doing this. could anyone
point me in the right direction or even just explain to me what i need
to do?

thanks,
 
J

Jason Hales

Hi Bazzar, it's pretty straight forward. Just create some LinkButtons
inside your Page_Load event and add these to the Panel's Controls
collection.

You must remember to add them even if it's a page PostBack otherwise
the Command events won't get fired when you click an item.

I've used the CommandArgument so set an ID as you'll need to know
which one was fired and they all go to the same event handler
button_Command:

private void Page_Load(object sender, System.EventArgs e)
{
int numLinks = 10;
for(int i = 0; i < numLinks; i++)
{
LinkButton button = new LinkButton();
button.Command += new CommandEventHandler(button_Command);
button.CommandArgument = i.ToString();
button.Text = "Entry " + i;
Panel1.Controls.Add(button);
Panel1.Controls.Add(new LiteralControl("<br/>"));
}
}

private void button_Command(object sender, CommandEventArgs e)
{

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top