C# 如何检查页面加载时是否单击了 ASP.NET 按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18798940/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:14:16  来源:igfitidea点击:

How to check whether ASP.NET button is clicked or not on page load

c#asp.net

提问by user1030181

How can I check whether a particular button was clicked or not in ASP.NET?

如何检查在 ASP.NET 中是否单击了特定按钮?

I think I need to perform some operation on Page_Load. This shouldn't be entering to Button_Clickevent to find. Is there any way that I can find where it was clicked or not on Client Side and take it to Page_Load?

我想我需要对Page_Load. 这不应该是进入Button_Click事件来查找。有什么方法可以找到它在客户端上被点击或未被点击的位置并将其带到Page_Load

采纳答案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). 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.

我刚遇到同样的麻烦,不得不在 Page_Load 方法中做一些逻辑判断来处理不同的事件(点击了哪个按钮)。我实现了手臂以获取如下示例。前端 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. In your situation, the button be clicked will be added into dic. I hope that will be helpful to some one.

后端aspx.cs源码,我需要做的是判断Page_Load触发时点击了哪个按钮。这似乎有点愚蠢,但有效。在你的情况下,被点击的按钮将被添加到 dic 中。我希望这会对某人有所帮助。

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()); 
                    }
}

回答by backslash17

The UniqueID of the button will be in Request.Form["__EVENTTARGET"]

按钮的 UniqueID 将在 Request.Form["__EVENTTARGET"] 中

This question is already answered at: ASP.NET : Check for click event in page_load

此问题已在以下位置回答:ASP.NET : Check for click event in page_load

回答by karz

    private bool button1WasClicked = false;

    private void button1_Click(object sender, EventArgs e)
    {
        button1WasClicked = true;
    }


if ( button1WasClicked== false)
{
//do somthing
}

回答by R.C

Background:Basically __EVENTTARGETand __EVENTARGUMENT, These two Hidden controls are added to the HTML source, when ever any autopostback attribute is set to true for any of the web control.

背景:基本上 __EVENTTARGET__EVENTARGUMENT,这两个隐藏控件被添加到HTML源,当过任何的AutoPostBack属性被设置为true任何网络控制的。

The __EVENTTARGEThidden variable will tell the server ,which control actually does the server side event firing so that the framework can fire the server side event for that control.

__EVENTTARGET隐变量将告诉服务器,它控制实际执行服务器端事件触发,使这个框架可以触发该控制服务器端事件。

The __ EVENTARGUMENTvariable is used to provide additional event information if needed by the application, which can be accessed in the server.

__ EVENTARGUMENT如果应用程序需要,该变量用于提供额外的事件信息,这些信息可以在服务器中访问。

So we can easily get the control causing postback using:Request.Params.Get("__EVENTTARGET");

因此,我们可以使用以下方法轻松获得导致回发的控制:Request.Params.Get("__EVENTTARGET");

PROBLEM:

问题:

The method: Request.Params.Get("__EVENTTARGET");will work for CheckBoxes, DropDownLists, LinkButtons, etc.. but this does not work for Button controls such as Buttonsand ImageButtons

方法:Request.Params.Get("__EVENTTARGET");将适用于CheckBoxesDropDownListsLinkButtons等......但这不适用于按钮控件,例如ButtonsImageButtons

The Buttoncontrols and ImageButtoncontrols does not call the __doPostBackfunction. Because of this, the _EVENTTARGETwill always be empty. However, other controls uses javascript function __doPostBackto trigger postback.

Button控件和ImageButton控件不调用__doPostBack函数。正因为如此,_EVENTTARGET将永远是空的。但是,其他控件使用 javascript 函数 __doPostBack来触发回发。

So, I will suggest to do something as below. Add an OnClientClickproperty to the buttons. Also, define a hiddenField in your Markup, whose value will contain the actual button causing postback.

所以,我会建议做一些如下的事情。OnClientClick为按钮添加一个属性。此外,在您的标记中定义一个 hiddenField,其值将包含导致回发的实际按钮。

<asp:Button ID="Button1" runat="server" Text="Button"
     OnClientClick = "SetSource(this.id)" />
<asp:ImageButton ID="ImageButton1" runat="server"
     OnClientClick = "SetSource(this.id)" />
<asp:HiddenField ID="hidSourceID" runat="server" />

On the OnClientClickproperty of the Buttonand ImageButtonCall the SetSourceJavaScript function

关于和调用JavaScript 函数的OnClientClick属性ButtonImageButtonSetSource

<script type = "text/javascript">
    function SetSource(SourceID)
    {
        var hidSourceID =
        document.getElementById("<%=hidSourceID.ClientID%>");
        hidSourceID.value = SourceID;
    }
</script>

Here onwards, you can very easily check in your Page_Loadas to which Control caused postback:

从这里开始,您可以非常轻松地检查Page_Load哪个控件导致回发:

if (IsPostBack)
{
    string CtrlName;
    CtrlName=hidSourceID.Value;
}

回答by JulyOrdinary

You can try using the hidden field. Make the client side event on the OnclientClick event and try setting the value of hidden field, may be true or false depending on the condition. And on the page load you can check the value of Hiidden field.

您可以尝试使用隐藏字段。在 OnclientClick 事件上创建客户端事件并尝试设置隐藏字段的值,根据条件可能为 true 或 false。在页面加载时,您可以检查隐藏字段的值。

function click()
{
// set the hidden field here
}

And on the page load, simply check the value.

在页面加载时,只需检查值。

if(HiddenFieldName.Value=="true")
{
//perform the action
}