确定哪个控件引发回发
有什么想法如何检查 ASP.NET Web 应用程序中的哪个控件引发回发吗?
我有许多按钮,并且希望根据单击的按钮在 Page_Load 方法中执行不同的任务。
Any ideas how to check which control in an asp.net web application raised a postback?
I have a number of buttons, and want to perform a different task in the Page_Load method depending which button was clicked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要检查哪个控件导致回发,请使用 Request.Form["__EVENTTARGET"]。这应该返回导致回发的控件的唯一 ID。
编辑
为此,您必须将按钮的
UseSubmitBehavior
属性设置为 false,这会导致它使用 Asp Net 回发机制来自 MSDN
To check which control caused the postback, use Request.Form["__EVENTTARGET"]. This should return a unique ID of the control that caused the postback.
EDIT
For this to work you will have to set
UseSubmitBehavior
property of the button to false which causes it to use Asp Net post back mechanismFrom MSDN
您可以从请求中的“__EVENTTARGET”值获取回发控件ID。 params
请查看以下文章。
You can get the Postback control id from the '__EVENTTARGET' value in the requist. params
Have a look at the following article.
在按钮的方法处理程序中,事件包含对按钮的引用,以便您可以获取控件 ID:
EventArgs 参数包含命令名称,可用于识别您需要执行的操作:
Well in the method handler of the button the event it contains a reference to the button so you can get to the controls id:
the EventArgs parameter contains the command name which can be used to identify what you need to do: