Can you set EventArgs from ASP.NET controls?

1.4k Views Asked by At

I have a DropDownList whose SelectedIndexChanged event I set:

move.SelectedIndexChanged += Move;

public void Move(object sender, EventArgs e)
{
}

I'd like to create a class derived from EventArgs which is the argument "e" that gets passed to the Move method.

public void Move(object sender, EventArgs e)
{
  MyEventArgs my_e = (MyEventArgs)e;

  int Id = my_e.ID;
  position = my_e.position;
  etc;
}

Is that possible? If it were, I could give this class properties that I could use in the event:

I need to do this as I'd like to pass more information to the Move method than a DropDownList currently contains. I can put this information in the ID of the DropDownList but that is ugly and requires messy string parsing.

eg

ID = "Id_4_position_2"

Note: As requested by Developer Art

I am moving elements in a list. I need to know the old order and the new order. I can do that by using the DropDownList ID to store the old order and SelectedValue to store the new order. So actually, I have all I need but it seems inelegant to put an order in an ID. I also want to avoid custom events, seems too much work.

alt text http://www.yart.com.au/stackoverflow/move.png

1

There are 1 best solutions below

3
On BEST ANSWER

That's not possible.

You cannot cast a parent class to a child class.

You mean something like this:

public class MyEventArgs : EventArgs
{
}

Then you have EventArgs e and you wish to typecast it to the MyEventArgs. That won't work.

A child class extends the parent class which means it has more meat. The parent class is narrower. There is no sensible way to somehow automatically extend a parent class object to become a child class object.


What if you subclass the UI control and add a custom property to it?

public class PositionDropDownList : DropDownList
{
    public int Id { get; set; }
    public int Position { get; set; }
}

You set these values when you generate controls for your form. Then you can get to them in your event:

public void Move(object sender, EventArgs e)
{
  PositionDropDownList ddl = (PositionDropDownList)sender;

  int Id = ddl.Id;
  position = ddl.Position;
}

Would something like that work for you?