ASP.NET:检查page_load中的click事件

在c#中,如何检查页面加载方法中是否已单击链接按钮?

我需要知道在点击事件被触发之前是否被点击了。

if( IsPostBack ) { // get the target of the post-back, will be the name of the control // that issued the post-back string eTarget = Request.Params["__EVENTTARGET"].ToString(); } 

按钮的UniqueID将在Request.Form [“__ EVENTTARGET”]中

检查请求参数__EVENTTARGET的值,以查看它是否是相关链接按钮的ID。

如果这不起作用。尝试UseSubmitBehavior="false"

基于已接受的答案和RealSteel的答案,这可能是一个更完整的答案。

首先添加.aspx按钮,如下所示:

  

然后在Page_Load方法上:

 if(IsPostBack){ var eventTarget = Request.Params["__EVENTTARGET"] // Then check for the id but keep in mind that the name could be // something like ctl00$ContainerName$btnExport // if the button was clicked or null so take precautions against null // ... so it could be something like this var buttonClicked = eventTarget.Substring(eventTarget.LastIndexOf("$") + 1).Equals("btnExport") } 

我遇到了同样的麻烦,必须在Page_Load方法中做一些逻辑判断来处理不同的事件(点击了哪个按钮)。

我意识到arm得到如下例子。

前端aspx源代码(我有许多ID为F2,F3,F6,F12的按钮。

      

后端aspx.cs源代码,我需要做的是判断触发Page_Load时点击了哪个按钮。 这似乎有点愚蠢,但有效。 我希望这会对其他人有所帮助

 Dictionary dic = new Dictionary(); foreach(var id in new string[]{"F2","F3","F6","F12"}) { foreach (var key in Request.Params.AllKeys) { if (key != null && key.ToString().Contains(id)) dic.Add(id, Request[key.ToString()].ToString()); } }