|
-
Apr 10th, 2007, 04:31 PM
#1
Thread Starter
Addicted Member
[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
-
Apr 10th, 2007, 06:19 PM
#2
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.
-
Apr 11th, 2007, 01:12 AM
#3
Fanatic Member
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?
-
Apr 11th, 2007, 01:42 AM
#4
Re: exactly what does this mean and can it go both ways?
 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:
''' <summary>
''' Provides data for an event raised to indicate that a text value is changing.
''' </summary>
''' <remarks>
''' Provides an opportunity to cancel the change if the new value is not appropriate.
''' </remarks>
Public Class StringChangeCancelEventArgs
Inherits System.ComponentModel.CancelEventArgs
#Region " Variables "
''' <summary>
''' The current value of the text.
''' </summary>
Private _currentValue As String
''' <summary>
''' The new value of the text.
''' </summary>
Private _proposedValue As String
#End Region 'Variables
#Region " Properties "
''' <summary>
''' Gets the current value of the text.
''' </summary>
''' <value>
''' A <b>String</b> containing the current value of the text.
''' </value>
Public ReadOnly Property CurrentValue() As String
Get
Return _currentValue
End Get
End Property
''' <summary>
''' Gets the new value of the text.
''' </summary>
''' <value>
''' A <b>String</b> containing the new value of the text.
''' </value>
Public ReadOnly Property ProposedValue() As String
Get
Return _proposedValue
End Get
End Property
#End Region 'Properties
#Region " Constructors "
''' <summary>
''' Initialises a new instance of the <see cref="StringChangeCancelEventArgs"/> class.
''' </summary>
''' <param name="proposedValue">
''' The value the text will have after the change.
''' </param>
''' <param name="currentValue">
''' The current value of the text.
''' </param>
Public Sub New(ByVal proposedValue As String, ByVal currentValue As String)
Me._proposedValue = proposedValue
Me._currentValue = currentValue
End Sub
#End Region 'Constructors
End Class
and here's my event that uses it:
VB Code:
''' <summary>
''' Occurs when the <see cref="Name"/> property value is changing.
''' </summary>
''' <remarks>
''' Provides an opportunity to cancel a name change if the proposed name is already in use.
''' </remarks>
Public Event NameChanging As EventHandler(Of StringChangeCancelEventArgs)
Here's the method that raises the event:
VB Code:
''' <summary>
''' Raises the <see cref="NameChanging"/> event.
''' </summary>
''' <param name="e">
''' A <see cref="StringChangeCancelEventArgs"/> object containing the data for the event.
''' </param>
Protected Overridable Sub OnNameChanging(ByVal e As StringChangeCancelEventArgs)
RaiseEvent NameChanging(Me, e)
End Sub
and here's the property that calls that method:
VB Code:
''' <summary>
''' Gets or sets the name of the button layout.
''' </summary>
''' <value>
''' A <b>String</b> containing the name of the button layout.
''' </value>
Public Property Name() As String
Get
Return Me._name
End Get
Set(ByVal value As String)
If Me._name <> value Then
Dim e As New StringChangeCancelEventArgs(value, Me._name)
Me.OnNameChanging(e)
'Update the value if and only if the new value is different and the change is not cancelled.
If Not e.Cancel Then
Me._name = value
Me.OnNameChanged(EventArgs.Empty)
End If
End If
End Set
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:
''' <summary>
''' Handles the <see cref="ButtonLayout.NameChanging">NameChanging</see> event for all items in the collection.
''' </summary>
Private Sub ButtonLayout_NameChanging(ByVal sender As Object, ByVal e As StringChangeCancelEventArgs)
Dim item As ButtonLayout = TryCast(sender, ButtonLayout)
If item IsNot Nothing Then
Dim name As String = e.ProposedValue
'Cancel the change if an existing item already has the proposed name.
If Me.IndexOfName(name) <> -1 Then
e.Cancel = True
Me.OnDuplicateNameRejected(New DuplicateNameRejectedEventArgs(name))
End If
End If
End Sub
-
Apr 11th, 2007, 10:52 AM
#5
Thread Starter
Addicted Member
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
-
Apr 11th, 2007, 10:55 AM
#6
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?
-
Apr 11th, 2007, 11:09 AM
#7
Thread Starter
Addicted Member
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);
}
-
Apr 11th, 2007, 06:31 PM
#8
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?
-
Apr 11th, 2007, 06:39 PM
#9
Thread Starter
Addicted Member
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...
-
Apr 11th, 2007, 06:47 PM
#10
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.
-
Apr 11th, 2007, 07:00 PM
#11
Thread Starter
Addicted Member
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>
-
Apr 11th, 2007, 07:20 PM
#12
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.
-
Apr 12th, 2007, 12:58 PM
#13
Thread Starter
Addicted Member
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??
-
Apr 12th, 2007, 05:41 PM
#14
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.
-
Apr 13th, 2007, 10:48 AM
#15
Thread Starter
Addicted Member
Re: exactly what does this mean and can it go both ways?
thats better now i understand thank you
-
Apr 19th, 2007, 04:48 PM
#16
I wonder how many charact
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|