Multiple LinkButton and Event Handlers in a Repeater

Discussion in 'ASP .Net Web Controls' started by mikelor, Jul 16, 2004.

  1. mikelor

    mikelor Guest

    Hi,
    I have a repeater control with more than one LinkButton. I know I can
    use the ItemCommand event handler for the repeater control to catch the
    LinkButton Command events as long as I give each LinkButton a unique
    command name I can do something like this

    private void MyRepeater_ItemCommand( Object o, RepeaterEventArgs e )
    {
    switch( e.CommandName )
    {
    case "Edit":
    DoEdit();

    case "Delete"
    DoDelete();
    }
    }

    However what I would like to do is have a event handler for each
    LinkButton Command Event

    private void EditLinkButton_Command( Object o, CommandEventArgs e )
    {
    DoEdit();
    }

    private void DeleteLinkButton_Command( Object o, CommandEventArgs e )
    {
    DoDelete();
    }

    The second way seems a bit cleaner to me. However when I try and add
    the event handler I get a null reference exception on the
    editLinkButton object

    this.editLinkButton.Command += new CommandEventHandler(
    this.EditLinkButton_Command )

    Even though I have
    <asp:LinkButton id="editLinkButton" ...>Edit</asp> in my repeater and
    have delclared

    protected LinkButton editLinkButton;

    in my codebehind.

    Like I said, I can get it to work using the ItemCommand event handler,
    but it seems a bit messy to me to use the switch case to determin the
    object sending the event. However all code that I've searched for seems
    to do it this way..

    Any other ideas?
    -=mike
     
    mikelor, Jul 16, 2004
    #1
    1. Advertising

  2. mikelor

    Dale Guest

    Mike,

    What you're doing now, with the ItemCommand event and the switch block, is
    the clean way to do it.

    Since you can't predict how many link buttons there are going to be, it
    would be hard to do what you're talking about. You'd have to declare each
    button using the UniqueID and row number in the same way the repeater
    generates the IDs for the buttons, and then add the event handler to each
    button. To try to make that all work out would be a lot of work.

    The other option is to create your own repeater control derived from the
    System.Web.UI.WebControls.Repeater and handle the event handler assignments
    in your override of the OnItemCreated or OnItemDataBound events or you could
    create your own custom template implementing the ITemplate interface.

    All told, I would stick with the ItemCommand event and switch block.

    Dale



    "mikelor" <> wrote in message
    news:cd8u61$...
    > Hi,
    > I have a repeater control with more than one LinkButton. I know I can
    > use the ItemCommand event handler for the repeater control to catch the
    > LinkButton Command events as long as I give each LinkButton a unique
    > command name I can do something like this
    >
    > private void MyRepeater_ItemCommand( Object o, RepeaterEventArgs e )
    > {
    > switch( e.CommandName )
    > {
    > case "Edit":
    > DoEdit();
    >
    > case "Delete"
    > DoDelete();
    > }
    > }
    >
    > However what I would like to do is have a event handler for each
    > LinkButton Command Event
    >
    > private void EditLinkButton_Command( Object o, CommandEventArgs e )
    > {
    > DoEdit();
    > }
    >
    > private void DeleteLinkButton_Command( Object o, CommandEventArgs e )
    > {
    > DoDelete();
    > }
    >
    > The second way seems a bit cleaner to me. However when I try and add
    > the event handler I get a null reference exception on the
    > editLinkButton object
    >
    > this.editLinkButton.Command += new CommandEventHandler(
    > this.EditLinkButton_Command )
    >
    > Even though I have
    > <asp:LinkButton id="editLinkButton" ...>Edit</asp> in my repeater and
    > have delclared
    >
    > protected LinkButton editLinkButton;
    >
    > in my codebehind.
    >
    > Like I said, I can get it to work using the ItemCommand event handler,
    > but it seems a bit messy to me to use the switch case to determin the
    > object sending the event. However all code that I've searched for seems
    > to do it this way..
    >
    > Any other ideas?
    > -=mike
    >
     
    Dale, Jul 18, 2004
    #2
    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. Michael
    Replies:
    1
    Views:
    2,638
    Michael
    Dec 7, 2004
  2. Replies:
    2
    Views:
    2,204
    Smash
    Aug 5, 2005
  3. Sathyaish
    Replies:
    3
    Views:
    3,118
    Sathyaish
    Sep 8, 2005
  4. Sathyaish

    Linkbutton does not look like a linkbutton

    Sathyaish, Sep 8, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    194
    Sathyaish
    Sep 8, 2005
  5. Tumurbaatar S.

    Event handlers called multiple times

    Tumurbaatar S., Nov 11, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    200
    Tumurbaatar S.
    Nov 11, 2005
Loading...

Share This Page