Results 1 to 16 of 16

Thread: [RESOLVED] exactly what does this mean and can it go both ways?

  1. #1

    Thread Starter
    Addicted Member KingSatan's Avatar
    Join Date
    Feb 2006
    Posts
    185

    Resolved [RESOLVED] exactly what does this mean and can it go both ways?

    Button_Click(object, System.Web.UI.WebControls.DataGridCommandEventArgs)' does not match delegate 'void System.EventHandler(object, System.EventArgs)'

    it only appears when i add DatagridEventCommandArgs to my subroutine like this

    Code:
    		private void btnSpecials_Click(object sender, System.EventArgs e, DatagridEventCommandArgs d)
    		{
    		SomeOtherVoid(d);
    		}

    can i have it both ways? or is there some wierd thing i missed?
    Last edited by KingSatan; Apr 11th, 2007 at 10:51 AM. Reason: sorry i messed something up

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: exactly what does this mean and can it go both ways?

    Every event has a signature, i.e. an argument list. When the event is raised those argument values are created and passed to every method that handles that event. If the method doesn't have the same signature than how can those parameters be passed to it?

    If an event has the signature:
    Code:
    void System.EventHandler(object, System.EventArgs)
    then every method that handles it must have two arguments: one an Object and the other an EventArgs.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    Re: exactly what does this mean and can it go both ways?

    @jmcilhinney, does that mean we can define a event handler accepting three arguments?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: exactly what does this mean and can it go both ways?

    Quote Originally Posted by rjv_rnjn
    @jmcilhinney, does that mean we can define a event handler accepting three arguments?
    As I said, the signature of the event handler must match the signature of the event. If you've declared the event yourself then you can make the signature anything you want.

    Having said that, I very, VERY strongly suggest that you follow the convention and declare EVERY event with two arguments only: the first ALWAYS an Object named "sender" and the second ALWAYS an EventArgs (or a type that inherits EventArgs) named "e". If you want to pass data to the event handler then you should be deriving, either directly or indirectly, your own type from EventArgs and passing an instance of that to the second argument. The following is VB code but gives you the idea of what I'm talking about. Here's my custom EventArgs type:
    VB Code:
    1. ''' <summary>
    2. ''' Provides data for an event raised to indicate that a text value is changing.
    3. ''' </summary>
    4. ''' <remarks>
    5. ''' Provides an opportunity to cancel the change if the new value is not appropriate.
    6. ''' </remarks>
    7. Public Class StringChangeCancelEventArgs
    8.     Inherits System.ComponentModel.CancelEventArgs
    9.  
    10. #Region " Variables "
    11.  
    12.     ''' <summary>
    13.     ''' The current value of the text.
    14.     ''' </summary>
    15.     Private _currentValue As String
    16.     ''' <summary>
    17.     ''' The new value of the text.
    18.     ''' </summary>
    19.     Private _proposedValue As String
    20.  
    21. #End Region 'Variables
    22.  
    23. #Region " Properties "
    24.  
    25.     ''' <summary>
    26.     ''' Gets the current value of the text.
    27.     ''' </summary>
    28.     ''' <value>
    29.     ''' A <b>String</b> containing the current value of the text.
    30.     ''' </value>
    31.     Public ReadOnly Property CurrentValue() As String
    32.         Get
    33.             Return _currentValue
    34.         End Get
    35.     End Property
    36.  
    37.     ''' <summary>
    38.     ''' Gets the new value of the text.
    39.     ''' </summary>
    40.     ''' <value>
    41.     ''' A <b>String</b> containing the new value of the text.
    42.     ''' </value>
    43.     Public ReadOnly Property ProposedValue() As String
    44.         Get
    45.             Return _proposedValue
    46.         End Get
    47.     End Property
    48.  
    49. #End Region 'Properties
    50.  
    51. #Region " Constructors "
    52.  
    53.     ''' <summary>
    54.     ''' Initialises a new instance of the <see cref="StringChangeCancelEventArgs"/> class.
    55.     ''' </summary>
    56.     ''' <param name="proposedValue">
    57.     ''' The value the text will have after the change.
    58.     ''' </param>
    59.     ''' <param name="currentValue">
    60.     ''' The current value of the text.
    61.     ''' </param>
    62.     Public Sub New(ByVal proposedValue As String, ByVal currentValue As String)
    63.         Me._proposedValue = proposedValue
    64.         Me._currentValue = currentValue
    65.     End Sub
    66.  
    67. #End Region 'Constructors
    68.  
    69. End Class
    and here's my event that uses it:
    VB Code:
    1. ''' <summary>
    2.     ''' Occurs when the <see cref="Name"/> property value is changing.
    3.     ''' </summary>
    4.     ''' <remarks>
    5.     ''' Provides an opportunity to cancel a name change if the proposed name is already in use.
    6.     ''' </remarks>
    7.     Public Event NameChanging As EventHandler(Of StringChangeCancelEventArgs)
    Here's the method that raises the event:
    VB Code:
    1. ''' <summary>
    2.     ''' Raises the <see cref="NameChanging"/> event.
    3.     ''' </summary>
    4.     ''' <param name="e">
    5.     ''' A <see cref="StringChangeCancelEventArgs"/> object containing the data for the event.
    6.     ''' </param>
    7.     Protected Overridable Sub OnNameChanging(ByVal e As StringChangeCancelEventArgs)
    8.         RaiseEvent NameChanging(Me, e)
    9.     End Sub
    and here's the property that calls that method:
    VB Code:
    1. ''' <summary>
    2.     ''' Gets or sets the name of the button layout.
    3.     ''' </summary>
    4.     ''' <value>
    5.     ''' A <b>String</b> containing the name of the button layout.
    6.     ''' </value>
    7.     Public Property Name() As String
    8.         Get
    9.             Return Me._name
    10.         End Get
    11.         Set(ByVal value As String)
    12.             If Me._name <> value Then
    13.                 Dim e As New StringChangeCancelEventArgs(value, Me._name)
    14.  
    15.                 Me.OnNameChanging(e)
    16.  
    17.                 'Update the value if and only if the new value is different and the change is not cancelled.
    18.                 If Not e.Cancel Then
    19.                     Me._name = value
    20.                     Me.OnNameChanged(EventArgs.Empty)
    21.                 End If
    22.             End If
    23.         End Set
    24.     End Property
    Now when the NameChanging event is raised the event handlers receive a StringChangeCancelEventArgs object. Here's how I use that object:
    VB Code:
    1. ''' <summary>
    2.     ''' Handles the <see cref="ButtonLayout.NameChanging">NameChanging</see> event for all items in the collection.
    3.     ''' </summary>
    4.     Private Sub ButtonLayout_NameChanging(ByVal sender As Object, ByVal e As StringChangeCancelEventArgs)
    5.         Dim item As ButtonLayout = TryCast(sender, ButtonLayout)
    6.  
    7.         If item IsNot Nothing Then
    8.             Dim name As String = e.ProposedValue
    9.  
    10.             'Cancel the change if an existing item already has the proposed name.
    11.             If Me.IndexOfName(name) <> -1 Then
    12.                 e.Cancel = True
    13.                 Me.OnDuplicateNameRejected(New DuplicateNameRejectedEventArgs(name))
    14.             End If
    15.         End If
    16.     End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member KingSatan's Avatar
    Join Date
    Feb 2006
    Posts
    185

    Re: exactly what does this mean and can it go both ways?

    sorry i accidentally placed DataGridEventCommandArgs twice i meant to set System.EventArgs e

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: exactly what does this mean and can it go both ways?

    So are you saying that you had EventArgs in the original code and it's still not working, or that you have no corrected the code and it works?

  7. #7

    Thread Starter
    Addicted Member KingSatan's Avatar
    Join Date
    Feb 2006
    Posts
    185

    Re: exactly what does this mean and can it go both ways?

    no i was trying to use the DGcomandEventArgs with the System.EventArgs like this

    Code:
    		private void btnSpecials_Click(object sender, System.EventArgs e, DatagridEventCommandArgs d)
    		{
    		SomeOtherVoid(d);
    		}

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: exactly what does this mean and can it go both ways?

    My original explanation stands. The Click event of the Button class has a specific signature. It provides an Object as the first parameter and an EventArgs as the second. Those are the parameters that the event supplies so those are the parameters that all event handlers for that event must receive. If the event only provides two parameters and your event handler has three arguments, where is the third parameter supposed to come from? Can you explain why you're trying to do this in the first place?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Addicted Member KingSatan's Avatar
    Join Date
    Feb 2006
    Posts
    185

    Re: exactly what does this mean and can it go both ways?

    ok theres the button_click void
    i need that button click to run a loop i made to loop thru my datagrid and add each row where the checkbox is checked to a different datagrid...
    in order to get the 2nd id (not the primary key) i had to make a command argument in asp... and the only way i know how to get to the command arg is
    Code:
    x.DatagridCommandEventArgs
    but i cant set x in my button_click void...

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: exactly what does this mean and can it go both ways?

    The sole purpose of the EventArgs class, and any class derived from the EventArgs class, is to store data that will be passed to the second argument of an event handler. While it's not illegal to do so, there are absolutely no circumstances where you should be creating an EventArgs object, or an instance of a derived class, for any other purpose. You should also only ever be receiving an EventArgs or derived object in a method that expressly exists to handle an event or in some rare cases where you actually pass the EventArgs object from the event handler to another method.

    Now, I don't know exactly how the WebForms DataGrid works because I've never coded WebForms, but if this was a WinForms app you could get all the information you needed by either looping through the DataGrid itself or its data source. You can't get any additional information provided to an event handler than the event provides, and a Button.Click event doesn't provide any information about a DataGrid.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Addicted Member KingSatan's Avatar
    Join Date
    Feb 2006
    Posts
    185

    Re: exactly what does this mean and can it go both ways?

    i used this to get the value in the ID cell of the currentIndex row
    which is why i needed to use d.DatagridCommandEvenargs....

    Code:
    					<asp:TemplateColumn>
    						<ItemTemplate>
    							<asp:LinkButton id=Linkbutton1 runat="server" Text="Delete" [COLOR="SandyBrown"]CommandArgument='<%# DataBinder.Eval(Container, "DataItem.intSpecialID") %>' [/COLOR]CommandName="Delete">
    							</asp:LinkButton>
    						</ItemTemplate>
    					</asp:TemplateColumn>

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: exactly what does this mean and can it go both ways?

    Like I said, I don't know much about WebForms but it looks to me like that is the value that gets passed to the appropriate event when you click on that cell of the grid. That has nothing to do with clicking an external button.

    Now you know that you can't arbitrarily add arguments to event handlers. You also know that you need to retrieve the value of interest from the DataGrid in the Button.Click event handler. Your question should be "how do I get a value from a DataGrid cell?" and it should probably be asked in the ASP.NET forum.
    Last edited by jmcilhinney; Apr 11th, 2007 at 07:24 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Addicted Member KingSatan's Avatar
    Join Date
    Feb 2006
    Posts
    185

    Re: exactly what does this mean and can it go both ways?

    i did get the value

    with the d.DatagridCommandeventArgs
    Code:
    i just used System.Convert.ToInt(d.DatagridCommandeventArgs)
    but what im wondering is there any way to allow the use of dg command args along with the use of e.eventargs??

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: exactly what does this mean and can it go both ways?

    I don't think you're actually hearing what I'm saying. A Button.Click event receives two parameters: an Object and an EventArgs. The Object is the Button itself and the EventArgs is created by the Button. It doesn't receive any different parameters and it doesn't receive any extra parameters.

    A DataGridCommandEventArgs is an object that is created by the DataGrid itself, not by you, and passed to the event handler for that DataGrid's CancelCommand, DeleteCommand, EditCommand, ItemCommand or UpdateCommand events. The handlers for all those events recieve two parameters: an Object and a DataGridCommandEventArgs. The Object is the DataGrid itself and the DataGridCommandEventArgs is created by the DataGrid and contains data relating to the event.

    When you click a Button its Click event is raised. A Button is not a DataGrid so it doesn't create any DataGridCommandEventArgs objects, thus you have no access to any DataGridCommandEventArgs objects in a Button.Click event handler.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Addicted Member KingSatan's Avatar
    Join Date
    Feb 2006
    Posts
    185

    Re: exactly what does this mean and can it go both ways?

    thats better now i understand thank you

  16. #16
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: exactly what does this mean and can it go both ways?

    You should state what you are trying to achieve.

    It sounds so far you have a datagrid with a list of 'items'. A user selects one or more items by checking a checkbox. They then click a button (outside the datagrid ?) to get the specials for those items?

    Normally, most web applications rely heavily on a single identifier - and so are built that way. So if you need to populate a grid from a database, you have a primary key, that identifies that item. When the user clicks 'get specials', you then iterate the items in the datagrid and collect a list of their respective primary keys. You then feed this to a call to the database, which gives you more results, that you then bind to the datagrid (more often than not, on a completely seperate page or a new instance of a user control which contains that grid). Even if you have a composite key, you can still use that as an identifier by simplying concatenating the two keys and delimiting them with a : perhaps. But you normally don't want to expose too much of the underlying database fields in the html source you pass over the internet to the public.

    You can always manually call the event handler you defined, passing it a DataGridCommandEventArgs object you created - but the ability to do so was intended for the benefit of WebControl development, not necessarily for Web page development.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width