How to add links in code-behind

asp.net

I need to add linked buttons in the code-behind based on a DataSet that is populated. How do I do that? Do I create a placeholder? I could be adding none, one, or many different linked buttons. I lalso need to have each linked button call a method in the code-behind. How would I wire that up when I add the button?

Thank you for any help.

Best Solution

Here's one way:

on the front end, you'd have some type of data-repeater control, for example a GridView.

In the ItemTemplate for said databound control, you'd declare your link button:

<asp:linkButton CommandName='Foo' CommandArgument='<%# Eval("Bar") %>'>
<%# Eval("Text") %>
</asp:linkButton>

Take note that I'm assigning it a Command Name, and binding a value into the CommandArugment for the button. That's important.

Then, on the server side, you'd wire up an event for the data-repeater's Click event.

Check the Event Args class for the Command Name & Argument to determine which function you need to access based on the LinkButton clicked.