asp.net:如何使用eventargs在按钮onclick上传递参数?

发布于 2024-07-18 13:20:56 字数 67 浏览 5 评论 0原文

我见过一些代码,其中人们通过Web控件的commandParameter属性传递参数。那么eventargs有什么用呢?

i have seen some code in which people pass parameter through commandParameter Property of web control.then what is use of eventargs.

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

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

发布评论

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

评论(3

两人的回忆 2024-07-25 13:20:56

如果不同的按钮具有相同的 EventHandler 方法,这会很有用。 例如,假设您的标记如下所示:

<asp:Button ID="button1" runat="server" CommandArgument="MyVal1"
   CommandName="ThisBtnClick" OnClick="MyBtnHandler" />
<asp:Button ID="button2" runat="server" CommandArgument="MyVal2"
   CommandName="ThatBtnClick" OnClick="MyBtnHandler" />

您可以为两个按钮使用相同的事件处理程序,并根据 CommandName 进行区分:

protected void MyBtnHandler(Object sender, EventArgs e)
{
     Button btn = (Button)sender;
          switch (btn.CommandName)
          {
                case "ThisBtnClick":
                    DoWhatever(btn.CommandArgument.ToString());
                    break;
                case "ThatBtnClick":
                    DoSomethingElse(btn.CommandArgument.ToString());
                    break;
           }
}

来源: aspnet-passing-parameters-in-button-click-handler

This can be useful if you have the same EventHandler method for different buttons. Example, say your markup looks like this:

<asp:Button ID="button1" runat="server" CommandArgument="MyVal1"
   CommandName="ThisBtnClick" OnClick="MyBtnHandler" />
<asp:Button ID="button2" runat="server" CommandArgument="MyVal2"
   CommandName="ThatBtnClick" OnClick="MyBtnHandler" />

You can have the same event handler for both buttons and differentiate based on the CommandName:

protected void MyBtnHandler(Object sender, EventArgs e)
{
     Button btn = (Button)sender;
          switch (btn.CommandName)
          {
                case "ThisBtnClick":
                    DoWhatever(btn.CommandArgument.ToString());
                    break;
                case "ThatBtnClick":
                    DoSomethingElse(btn.CommandArgument.ToString());
                    break;
           }
}

Source: aspnet-passing-parameters-in-button-click-handler

半夏半凉 2024-07-25 13:20:56

.NET 中的各种按钮类型控件都有一个 OnCommand 事件以及一个 OnClick 事件。 使用 OnCommand 事件时,您可以将其他参数应用于按钮,例如 CommandNameCommandArgument。 然后可以在 CommandEventArgs 中访问这些内容。

当您想要将相同的方法分配给多个按钮并使用 CommandNameCommandArgument 参数来指示单击该按钮将产生什么功能时,它非常有用。

Various Button type controls in .NET have an OnCommand event as well as an OnClick event. When using the OnCommand event you have additional parameters you can apply to the Button such as CommandName and CommandArgument. These can then be accessed in the CommandEventArgs.

It is useful in places where you want to assign the same method to multiple buttons and use the CommandName and CommandArgument parameters to indicate what functionality clicking that button will produce.

稳稳的幸福 2024-07-25 13:20:56

EventArgs 类是一个基类。 其他 Web 方法使用其他事件参数。

例如,LinkBut​​ton 有一个带有默认 EventArgs 参数的 OnClick 事件,但它也有一个带有 CommandEventArgs : EventArgs 参数的 OnCommand 事件,该参数具有一些额外信息(即 CommandName 和 CommandArgument)。

事件参数基类只是作为占位符,以便所有事件处理程序都符合类似的签名。

The EventArgs class is a base class. Other web methods use other Event args.

e.g. a LinkButton has an OnClick event with a default EventArgs parameter, but it also has an OnCommand event that takes a CommandEventArgs : EventArgs parameter which has some extra information (namely CommandName & CommandArgument).

The event args base class is just there as a place holder so that all the EventHandlers conform to a similar signature.

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