Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Multiple LinkButton and Event Handlers in a Repeater

Reply
Thread Tools

Multiple LinkButton and Event Handlers in a Repeater

 
 
mikelor
Guest
Posts: n/a
 
      07-16-2004
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

 
Reply With Quote
 
 
 
 
Dale
Guest
Posts: n/a
 
      07-18-2004
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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Internet Explorer Multiple Event Handlers Denial of Service Au79 Computer Support 0 03-21-2006 05:20 AM
Linkbutton does not look like a linkbutton Sathyaish ASP .Net 3 09-08-2005 09:41 AM
Linkbutton does not look like a linkbutton Sathyaish ASP .Net Datagrid Control 1 09-08-2005 08:44 AM
How to create event when I put linkbutton inside repeater? smash2004@gmail.com ASP .Net 2 08-05-2005 06:01 AM
How to trap click event of LinkButton inside a Repeater? Michael ASP .Net 1 12-07-2004 08:58 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57