0

I have the following line in my code, inside the click event handler of an ImageButton:

Protected Sub FinaliseBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FinaliseBtn.Click, SubmitPaymentViaChequeBtn.Click
   Dim str as String = sender.commandargument.ToString.ToLower
End Sub

Both Controls are ImageButton's. However, I'm getting the following error:

Property 'CommandArgument' is WriteOnly.

Can anyone see why I'm getting this error as usually I can read from a CommandArgument inside an event handler. In fact, surely thats their main use!

Thanks.

Curtis
  • 101,612
  • 66
  • 270
  • 352
  • 1
    The error is not "ReadOnly" but "WriteOnly", means that property has setter only, not getter – ika Oct 29 '10 at 08:41
  • @ika urgh good point, sorry for wasting time. I'll rewrite my question. – Curtis Oct 29 '10 at 09:10
  • what type of control is this? and can you show your method signature? BTW - `sender` is usually an `Object`, you should be accessing the `CommandEventArgs` (normally aliases as `e`) – RPM1984 Oct 29 '10 at 09:16
  • @RPM1984 Thanks for your comment, I've edited my question to show the Sub. – Curtis Oct 29 '10 at 09:29
  • @RPM1984 If the sending Object has a CommandArgument attribute, whats wrong with Object.CommandArgument? :S – Curtis Oct 29 '10 at 09:40
  • @RPM1984 Also, e.CommandArgument is not a member of System.EventArgs :S. Thanks for your help with this so far though, much appreciated! – Curtis Oct 29 '10 at 09:41
  • @Curt - yes, sorry, im thinking of LinkButton (CommandEventArgs). Try my answer out – RPM1984 Oct 29 '10 at 09:43

1 Answers1

0

You've wired up an event for EventArgs, but trying to retrieve CommandArgs.

This should be your method:

Sub ImageButton_Command(sender As Object, e As CommandEventArgs) 
         If (e.CommandName = "Sort") And (e.CommandArgument = "Ascending") Then
            Label1.Text = "You clicked the Sort Ascending Button"
         Else
            Label1.Text = "You clicked the Sort Descending Button"
         End If
      End Sub
RPM1984
  • 72,246
  • 58
  • 225
  • 350
  • I've tried the following: Dim _btn As ImageButton = TryCast(sender, ImageButton) Select Case _btn.CommandArgument However, I get the error: Object reference not set to an instance of an object. Cheers – Curtis Oct 29 '10 at 09:47
  • Sorry, it looks like one of my buttons is a UserControl containing an ImageButton, not just 2 ImageButtons! However, this answer helped me find that solution, thanks :D – Curtis Oct 29 '10 at 09:53
  • Yes, that would have been helpful information previously. :) Glad you got it sorted. – RPM1984 Oct 29 '10 at 09:57