确定哪个控件引发回发

发布于 2024-12-17 05:39:12 字数 95 浏览 4 评论 0原文

有什么想法如何检查 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 技术交流群。

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

发布评论

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

评论(3

╄→承喏 2024-12-24 05:39:12

要检查哪个控件导致回发,请使用 Request.Form["__EVENTTARGET"]。这应该返回导致回发的控件的唯一 ID。

编辑
为此,您必须将按钮的 UseSubmitBehavior 属性设置为 false,这会导致它使用 Asp Net 回发机制

使用 UseSubmitBehavior 属性指定 Button 控件是使用客户端浏览器的提交机制还是 ASP.NET 回发机制。默认情况下,此属性的值为 true,导致 Button 控件使用浏览器的提交机制。如果指定 false,ASP.NET 页面框架会将客户端脚本添加到页面以将表单发布到服务器。

当 UseSubmitBehavior 属性为 false 时,控件开发人员可以使用 GetPostBackEventReference 方法返回 Button 的客户端回发事件。 GetPostBackEventReference 方法返回的字符串包含客户端函数调用的文本,可以插入到客户端事件处理程序中。

来自 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 mechanism

Use the UseSubmitBehavior property to specify whether a Button control uses the client browser's submit mechanism or the ASP.NET postback mechanism. By default the value of this property is true, causing the Button control to use the browser's submit mechanism. If you specify false, the ASP.NET page framework adds client-side script to the page to post the form to the server.

When the UseSubmitBehavior property is false, control developers can use the GetPostBackEventReference method to return the client postback event for the Button. The string returned by the GetPostBackEventReference method contains the text of the client-side function call and can be inserted into a client-side event handler.

From MSDN

心凉 2024-12-24 05:39:12

您可以从请求中的“__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.

绳情 2024-12-24 05:39:12

在按钮的方法处理程序中,事件包含对按钮的引用,以便您可以获取控件 ID:

protected void Button1_Click(object sender, EventArgs e)
{
  ((System.Web.UI.WebControls.Button)sender).ID
}

EventArgs 参数包含命令名称,可用于识别您需要执行的操作:

if (e.CommandName == "AddToCart")
{   
    Do something
}

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:

protected void Button1_Click(object sender, EventArgs e)
{
  ((System.Web.UI.WebControls.Button)sender).ID
}

the EventArgs parameter contains the command name which can be used to identify what you need to do:

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