可以从 ASP.NET 控件设置 EventArgs 吗?
我有一个 DropDownList,我设置了其 SelectedIndexChanged 事件:
move.SelectedIndexChanged += Move;
public void Move(object sender, EventArgs e)
{
}
我想创建一个派生自 EventArgs 的类,该类是传递给 Move 方法的参数“e”。
public void Move(object sender, EventArgs e)
{
MyEventArgs my_e = (MyEventArgs)e;
int Id = my_e.ID;
position = my_e.position;
etc;
}
这可能吗? 如果是的话,我可以提供可以在事件中使用的此类属性:
我需要这样做,因为我想向 Move 方法传递比 DropDownList 当前包含的更多信息。我可以将此信息放入 DropDownList 的 ID 中,但这很丑陋并且需要混乱的字符串解析。
例如
ID = "Id_4_position_2"
注意:根据开发者艺术的要求,
我正在移动列表中的元素。我需要知道旧订单和新订单。我可以通过使用 DropDownList ID 来存储旧订单并使用 SelectedValue 来存储新订单来实现这一点。所以实际上,我已经拥有了我需要的一切,但在 ID 中下订单似乎不太雅观。我还想避免自定义事件,似乎工作太多。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那是不可能的。
您不能将父类强制转换为子类。
您的意思是这样的:
然后您有
EventArgs e
并且您希望将其类型转换为 MyEventArgs。那是行不通的。子类扩展了父类,这意味着它有更多的内容。父类范围更窄。没有明智的方法可以自动扩展父类对象以成为子类对象。
如果您子类化 UI 控件并向其添加自定义属性会怎样?
您可以在为表单生成控件时设置这些值。然后您可以在您的活动中联系他们:
这样的事情对您有用吗?
That's not possible.
You cannot cast a parent class to a child class.
You mean something like this:
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?
You set these values when you generate controls for your form. Then you can get to them in your event:
Would something like that work for you?