Dinamically assigning handler methods to controls on the page

G

Guest

Hi,
I'm building a screen where I want to allow users to be able to see data one
page at a time.
The users would view the data by clicking on the Page Nr.
The problem is that the number of pages is dynamic, based on how much data
the database returns, and I'm having troubles building the list of page
numbers.
I'm trying to build it using LinkButton controls, but because their number
is dynamic, I don't know how to link their handlers to them.
Or if I declare only one LinkButton and therefore only one handler for it,
how do I know which link was clicked?
What is the proper way to do it?

Thank you.
 
G

Guest

If you hook each LinkButton into the same Click event, the first parameter of
the EventHandler is the sender object. The sender object can be casted to
the LinkButton and you'll know exactly which LinkButton fired the event.

// Hooking into the click event
LinkButton[] buttons;
....
foreach(LinkButton button in buttons)
button.Click += new EventHandler(LinkButton_Click);

// Handling the event
private void LinkButton_Click(object sender, EventArgs e)
{
LinkButton button = sender as LinkButton;
if(button!=null)
{
Response.Output.WriteLine("{0} was clicked!", button.Text);
}
}

Hope that helps.
 
K

Karl Seguin

Vi,
I use a pager control for this type of stuff which I wrote. I've been using
it for a couple years, anyways, your aren' the first to ask a similar
question, so I've put it up with a little sample program. VB.Net version
coming over the weekend...hope it helps..

http://openmymind.net/pager/index.html

Karl
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top