C# ASP.NET:检查 page_load 中的点击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/188584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET : Check for click event in page_load
提问by user13053
In c#, how can I check to see if a link button has been clicked in the page load method?
在 C# 中,如何检查页面加载方法中是否单击了链接按钮?
I need to know if it was clicked before the click event is fired.
我需要知道它是否在点击事件触发之前被点击。
采纳答案by Jared
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();
}
回答by tvanfosson
Check the value of the request parameter __EVENTTARGET to see if it is the id of the link button in question.
检查请求参数 __EVENTTARGET 的值,看看它是否是相关链接按钮的 id。
回答by Tsvetomir Tsonev
The UniqueID of the button will be in Request.Form["__EVENTTARGET"]
按钮的 UniqueID 将在 Request.Form["__EVENTTARGET"] 中
回答by RealSteel
if that doesn't work.Try UseSubmitBehavior="false"
如果那不起作用。尝试 UseSubmitBehavior="false"
回答by Mike
Based on accepted answer and the answer of RealSteel this could be a more complete answer.
根据接受的答案和 RealSteel 的答案,这可能是一个更完整的答案。
First add to the .aspx a button like this:
首先向 .aspx 添加一个这样的按钮:
<asp:Button id="btnExport" runat="server" Text="Export" UseSubmitBehavior="false"/>
Then on the Page_Load method:
然后在 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")
}
回答by Wang Jijun
I just got the same trouble, have to do some logic judgement in the Page_Load method to treat different event(which button was clicked).
我刚遇到同样的麻烦,不得不在 Page_Load 方法中做一些逻辑判断来处理不同的事件(点击了哪个按钮)。
I realize the arm to get the as the following example.
我实现了手臂以获取如下示例。
The front end aspx source code(I have many Buttons with IDs F2, F3, F6, F12.
前端 aspx 源代码(我有许多 ID 为 F2、F3、F6、F12 的按钮。
<Button Style="display: none" ID="F2" runat="server" Text="F2:Cancel" OnClientClick="SeiGyo(this)" OnClick="F2_Click" />
<Button Style="display: none" ID="F3" runat="server" Text="F3:Return" OnClientClick="SeiGyo(this)" OnClick="F3_Click" />
<Button Style="display: none" ID="F6" runat="server" Text="F6:Run" OnClientClick="SeiGyo(this)" OnClick="F6_Click" />
<Button Style="display: none" ID="F12" runat="server" Text="F12:Finish" OnClientClick="SeiGyo(this)" OnClick="F12_Click" />
The back end aspx.cs source code, what I need to do is judge which button was clicked when Page_Load was triggered. It seems a little stupid, but works. I hope that will be helpful to some others
后端aspx.cs源码,我需要做的是判断Page_Load触发时点击了哪个按钮。这似乎有点愚蠢,但有效。我希望这会对其他人有所帮助
Dictionary<string, string> dic = new Dictionary<string, string>();
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());
}
}

