Multiple LinkButton and Event Handlers in a Repeater

M

mikelor

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
 
D

Dale

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
 

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,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top