可以从 ASP.NET 控件设置 EventArgs 吗?

发布于 2024-08-11 08:16:00 字数 920 浏览 5 评论 0原文

我有一个 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 中下订单似乎不太雅观。我还想避免自定义事件,似乎工作太多。

替代文本 http://www.yart.com.au/stackoverflow/move.png< /a>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想念有你 2024-08-18 08:16:00

那是不可能的。

您不能将父类强制转换为子类。

您的意思是这样的:

public class MyEventArgs : EventArgs
{
}

然后您有 EventArgs e 并且您希望将其类型转换为 MyEventArgs。那是行不通的。

子类扩展了父类,这意味着它有更多的内容。父类范围更窄。没有明智的方法可以自动扩展父类对象以成为子类对象。


如果您子类化 UI 控件并向其添加自定义属性会怎样?

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

您可以在为表单生成控件时设置这些值。然后您可以在您的活动中联系他们:

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

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

这样的事情对您有用吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文