Dinamically assigning handler methods to controls on the page

Discussion in 'ASP .Net' started by =?Utf-8?B?Vmk=?=, Nov 1, 2004.

  1. 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.
     
    =?Utf-8?B?Vmk=?=, Nov 1, 2004
    #1
    1. Advertising

  2. 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.

    --
    Jason Whitted



    "Vi" wrote:

    > 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.
     
    =?Utf-8?B?SldoaXR0ZWQ=?=, Nov 1, 2004
    #2
    1. Advertising

  3. =?Utf-8?B?Vmk=?=

    Karl Seguin Guest

    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

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Vi" <> wrote in message
    news:...
    > 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.
     
    Karl Seguin, Nov 2, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Umut Tezduyar
    Replies:
    2
    Views:
    550
    Umut Tezduyar
    Nov 10, 2003
  2. Rafi
    Replies:
    0
    Views:
    378
  3. oterox

    dinamically linkbuttons handler

    oterox, Jun 16, 2004, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    166
    Victor Garcia Aprea [MVP]
    Jun 18, 2004
  4. Replies:
    1
    Views:
    389
    Jan Tielens
    Jun 1, 2004
  5. weston
    Replies:
    1
    Views:
    271
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page