0

I have a ListView that has Update and Cancel buttons. Both of these buttons have a CommandName of Cancel, so they fire the same ListView event handler (ListView_ItemCanceling).

Inside this event handle I execute my stored procedures. The issue I am having is since both buttons fire the same event handler they both update. Even if there are no changes being made.

I would like to try to determine the button that has fired the event at the start of the event handler (possibly using sender?), but I cannot figure out how to do this.

This is what I was currently trying to do in the ListView_ItemCancelling event handler:

Button newButton = (Button)sender;
if(newButton.Text == "Cancel")
{
     Console.Write("this worked");
}

When I execute this code I get an error message telling me that I cannot convert the sender object from ListView object to a Button object.

Any help will be greatly appreciated!

Chase Ernst
  • 1,147
  • 1
  • 21
  • 53
  • I'm not sure if this is a solution, but [this SO answer](http://stackoverflow.com/a/1303176/1189566) might help you out. [this MSDN article](https://msdn.microsoft.com/en-us/library/cscsdfbt.aspx) sheds a little light on the `as` keyword, as opposed to trying to cast it like you are. Perhaps that will help. – sab669 Apr 09 '15 at 15:22
  • Hey, thanks for the readings. I was actually just reading that SO article, and tried using the AS keyword. When I do this, the `newButton` comes through as null then the if statement fails. – Chase Ernst Apr 09 '15 at 15:26
  • 1
    `sender` is not a button in this case, its your full listview. You need to use the [Item command](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemcommand(v=vs.110).aspx) event to know which button was pressed. – crthompson Apr 09 '15 at 15:29
  • 1
    ah damn. This is something I've been stumped on in the past and never found the solution I wanted and came up with some work arounds instead. Was hoping maybe that it would've helped – sab669 Apr 09 '15 at 15:29
  • I will give what @paqogomez said a try. Thanks for the help sab669 – Chase Ernst Apr 09 '15 at 15:31
  • @paqogomez if you submit your answer I will approve it! – Chase Ernst Apr 09 '15 at 16:11
  • @ChaseErnst thank you, but thats not a fully fleshed out answer that would be up to my standards. Why dont you post the code you used to solve it and use it as the answer? I'm sure it would be valuable. – crthompson Apr 09 '15 at 16:17
  • Alright, I can do that! Thanks for the help – Chase Ernst Apr 09 '15 at 16:18

3 Answers3

1

You can define to command names for each button to detect the which one is click for example: define the first as "Cancel1" and the other "Cancel2" and in the code you can check like that:

if(CommandName == "Cancel1")
{
// do some thing
}
else if(CommandName == "Cancel2")
{
// do other staff
}

or if both at doing the same job but you need to determine the sender

if(CommandName == "Cancel1" || CommandName == "Cancel2")
{
// do some thing common
}

if(CommandName == "Cancel1")
{
// do some thing if button 1 clicked
}

if(CommandName == "Cancel2")
{
// do some thing if button 2 clicked
}
Sherif Ahmed
  • 1,896
  • 1
  • 19
  • 37
1

I came to the answer with help from @paqogomez. He suggested I use the ItemCommand event handler for the ListView to get the button that is being clicked for the listview.

Inside the ItemCommand event handler I checked them command argument and used the appropriate code thereafter.

protected void LV_Tickets_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    if(e.CommandName == "Update")
    {
        //code here
    }
}
Chase Ernst
  • 1,147
  • 1
  • 21
  • 53
0

The sender seems to be your ListView, not Button. Try using Button_OnClick event instead of ListView_ItemCancelling.

Or try doing some reseach on ListView_ItemCancelling, such as using ListViewCancelEventArgs e parameter, maybe it can help you in this situation. You can read more about it on MSDN.

Roman Kotenko
  • 489
  • 7
  • 16
  • I am fully aware that the sender return the ListView and what the itemcancelling event is doing. I am just wondering how to get the buttons from this event handler. – Chase Ernst Apr 09 '15 at 15:42
  • @ChaseErnst I'm not sure, but it looks like the way to do it is to store pointers onto your buttons elsewhere and to work with them. But I don't know the whole code context, so maybe this solution doesn't fit you. – Roman Kotenko Apr 09 '15 at 15:53