Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting eventhandler name 1

Status
Not open for further replies.

SBGibson

Programmer
Apr 18, 2001
125
IT
Hi guys, I'm trying to give the possibility to a user to disable some event of a datagrid.
When an event is associated to a function in design time a line like that must be added to code:
this.dg.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_EditCommand);

to remove the handler we must do
this.dg.EditCommand -= new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_EditCommand);

The problem is: I would like to make a function in the page load that generically disable the events of a generical datagrid on the page.
I thougth to remove the eventhandler so I can
this.findcontrol(nameofthedatagrid).editcommand -= new System.Web.UI.WebControls.DataGridCommandEventHandler(???what???)

I don't know the name of the function added to eventhandler, how I can retrieve it?
I hope to have explained well the problem, maybe there's some other way to disable the event, the main thing is that it should be generical, in order to place it into a class from which deriving every webform.
Thank in advance

Stevie B. Gibson
 
Hmm I'm a little unsure of how/what you want here but I think you are looking to create a datagrid control which you can reuse across webforms/applications that allows the user (do you mean developer or user?) to disable certain events. Is that right?

I think the best way to do this would be to do a custom control which extends the DataGrids functionality and allows control over events. I haven't tested the following code at all so not sure if it works but it may give you something to think about as a way to do this
Code:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace ControlLibrary{
	
	public class LimitEventsDataGrid : DataGrid{

		private bool _IsAllowDeleteCommand = true;

		public bool IsAllowDeleteCommand{
			get{ return _IsAllowDeleteCommand; }
			set{ _IsAllowDeleteCommand = value; }
		}
		
		protected override void OnDeleteCommand(DataGridCommandEventArgs e) {
			if(_IsAllowDeleteCommand) base.OnDeleteCommand (e);
		}

		protected override void Render(HtmlTextWriter output){
			base.Render(output);
		}
	}
}
So the above is a Control which extends the DataGrid controls functionality. I have added a Property called IsAllowDeleteCommand and have overridden the OnDeleteCommand method to provide a custom implementation of the DeleteCommand. The custom implementation checks the value of IsAllowDeleteCommand and if it is allowed calls the base implementation of the event. You could extend this to include the events of the DataGrid you wish to have control over.

The developer can then assign allowed events either declaritively or programitically at design time or provide functionality in his web app to allow the user to apply different events at run time.

Let me know if this works as as I said its just chucked together and not tested.

Rob

Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
Your answer it's exactly the solution I'm keeping in the box for using it if I won't found another one. Why? Because I've an already developed application and I will have to change all the datagrid with my custom one.
As all my form inherits a custom webform class I thought to place in the OnInit event of my base webform class something that disables the event of the datagrid (or disables the commandbuttons contained in, but it seems more difficult), some way to modify the reactions of the datagrid depending on user configuration.
I thought that the user can tell me "datagrid1" and "edit off" and on the OnInit I remove the edit event handler from the datagrid "datagrid1" (found using findcontrol). The problem is that I'm not able to remove the handler.
Maybe there's another way to do the same thing, otherwise I'll use your suggestion that is cool, means a lot of work but is cool :)

Stevie B. Gibson
 
I mean end user, not developer (I missed you question)

Stevie B. Gibson
 
Ok Stevie, were stretching my experience a bit here but...

In the System.Component model namespace is an EventHandlerList collection. This is a protected property of all controls (i mean Page, UserControl, DataGrid, Button etc) as an Events collection. Heres what the framework docs have to say on the matter


Not enough! I figure as its protected this is no use either as you would have to create a custom DataGrid control to use it anyhow :-(

What sort of naming convention do you use for event handlers? The default from VS.NET would be
Code:
protected Button Button1;

Button1.Click += new EventHandler(Button1_Click);

private void Button1_Click(object sender, EventArgs e){ }
If you used the same convention then you could do
Code:
this.FindControl("DataGrid1").EditCommand -= new DataGridCommandEventHandler(DataGrid1_EditCommand);
I would have thought? Does that make sense or not?

Rob


Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
It's a cool problem right?
Your second solution is not right cause it assumes that in my form exist a DataGrid1_EditCommand function and I can call it directly. In this case I can do a
Code:
DataGrid1.EditCommand -= new DataGridCommandEventHandler(DataGrid1_EditCommand);
My goal is to make something like
Code:
this.FindControl("DataGrid1").EditCommand -= this.FindControl("DataGrid1").EditCommand.Handlers[0];
assuming that there's only one handler for that event.
The problem is that Handlers collection does not exists ehehe :)
Also
Code:
this.FindControl("DataGrid1").EditCommand -= 
new DataGridCommandEventHandler("DataGrid1_EditCommand");
it's not of help cause I cannot be sure that the name of the function suits the naming convention, I would like to act as there's no naming convention.
I know...I'm complicated :) Let's say that this would be the best, if it's not possible I'll use your first solution that seems the more elegant.
Btw thank you so much for your help.



Stevie B. Gibson
 
Hey no worries :)

Your right this is a cool problem and its been in and out of my mind all afternoon now. The Handlers collection does exist inside the Control as the protected Events collection I discussed earlier. This is not much use to you though!

I have had one more idea how you might be able to get this working. Reflection. I haven't had masses of experience using it so only know the basics but it does supply runtime information about objects. For example the System.Reflection.EventInfo class "Discovers the attributes of an event and provides access to event metadata" (MSDNs words not mine ;-)) and the MethodInfo class does the same for methods. Sounds promising...

My depth of knowledge in this area is limited and thats being generous. It's one of those aspects of the framework I find really intrigueing but never get a real world use for, yet, this does seem like one though.

Check out this posting on 247 from Microsoft Newsgroups


In the last (and accepted) answer the EventInfo and MethodInfo classes are used to add an event handler at runtime using the EventInfo.AddEventHandler method. The EventInfo class has a RemoveEventHandler method too so we could be in business here.

I'd be interested to hear what solution you implement in the end as as you say, it's a cool problem :)

Rob

Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
I'm looking heavily on reflection, unfortunately the post on 247 solves the problem if you know the name of the function ("DataGrid1_EditCommand") but without knowing it the thing is much complex.
In the meanwhile I solved another problem in which you have contributed, and I solved it using reflection. Look here [peace]

Stevie B. Gibson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top