简体   繁体   English

用户按下网页返回时如何执行事件

[英]How to execute event when user presses return on web page

I have an ASP.NET website and I have a forgot password page where the user enters their email address in a text box and when they click the button to retrieve password, the event runs as fine. 我有一个ASP.NET网站,并且有一个“忘记密码”页面,用户可以在其中输入文本框中的电子邮件地址,并且当他们单击按钮以检索密码时,事件运行得很好。

Only problem is if the user types in their email address and presses ENTER instead, it runs a search (I have a search bar at the top of the page) and so the result comes back as 'search query not found'. 唯一的问题是,如果用户输入其电子邮件地址并按ENTER键,它将运行搜索(页面顶部有一个搜索栏),因此结果以“未找到搜索查询”的形式返回。 But this search bar at the top is on a different ASP.NET page. 但是,此顶部的搜索栏位于另一个ASP.NET页上。

So anyway, I want the event onclick to run when the user presses enter and not run a search query. 因此,无论如何,我希望事件onclick在用户按下Enter键时运行,而不是运行搜索查询。 Does anyone have any ideas? 有人有什么想法吗? I've searched on this site but not really found the answer I need. 我已经在该网站上进行搜索,但并未真正找到所需的答案。

There are couple ways you can do this. 您可以通过几种方法来执行此操作。 If it is a webform and you have your textbox wrapped with an asp:panel you can do as below: 如果是网络表单,并且您的文本框用asp:panel包装,则可以执行以下操作:

<asp:Panel ID="p" runat="server" DefaultButton="myButton">
  <%-- Text boxes here --%>
  <asp:Button ID="myButton" runat="server" />
</asp:Panel>

If its not a webform or you want to move away from that try below: 如果它不是网络表单,或者您想退出该表单,请尝试以下操作:

<asp:TextBox ID="TextBox1" runat="server" onkeypress="return EnterEvent(event)"></asp:TextBox>    
<asp:Button ID="Button1" runat="server" style="display:none" Text="Button" />

function EnterEvent(e) {
        if (e.keyCode == 13) {
            __doPostBack('<%=Button1.UniqueId%>', "");
        }
    }

And in the codebehind fire your button click in the page load based on the parameters of the postback. 然后在后台代码中,基于回发的参数,点击页面加载中的按钮。

Specify the "defaultbutton" property to the ID of (event you want to fire). 将“ defaultbutton”属性指定为ID(您要触发的事件)。

You can specify the "defaultbutton" property at the Form level (in the form tag) 您可以在表单级别(在表单标签中)指定“ defaultbutton”属性

Or else you can define them at panel level in the tag. 否则,您可以在标签的面板级别定义它们。

The form level setting is overridden at the panel level setting. 表单级别设置在面板级别设置中被覆盖。 The Event Handler for the specified button gets fired simulating a true submit button functionality. 将触发指定按钮的事件处理程序,以模拟真正的提交按钮功能。

<form id="sampleform" runat="server" defaultbutton="button1">

<div>

<asp:TextBox ID="textBox1" runat="server"></asp:TextBox>

<asp:Button ID="button2" runat="server" Text="Cancel" OnClick="Button2_Click" />

<asp:Button ID="button1" runat="server" Text="Ok" OnClick="button1_Click" />

<asp:Panel ID="panel1" runat="server" defaultbutton="Button5">

<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>

<asp:Button ID="Button5" runat="server" Text="Button5" OnClick="Button5_Click" />

</asp:Panel>
</div>
</form>

In this example, Button1 is the default button for the form (Type something in Textbox 1 and hit enter, button1_Click gets fired). 在此示例中,Button1是该表单的默认按钮(在文本框1中键入内容,然后按Enter,将触发button1_Click)。 But for "Panel 1", default button will be Button 5 (Type in Textbox3 or Textbox 5 and hit enter). 但是对于“面板1”,默认按钮将是按钮5(在Textbox3或Textbox 5中键入,然后按Enter键)。 You can have any number of panels with different default button for each panel. 您可以具有任意数量的面板,每个面板具有不同的默认按钮。

Adding an ASP Panel around the text box and button with the id of the button name as the property for DefaultButton was the best way to do this. 最好的方法是在文本框和按钮周围添加一个ASP面板,并以按钮名称的ID作为DefaultButton的属性。

    <asp:Panel ID="p" runat="server" DefaultButton="myButton">
  <%-- Text boxes here --%>
  <asp:Button ID="myButton" runat="server" />
</asp:Panel>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户按下输入按钮wpf时,如何在键盘上触发按键事件 - How can I fire a key press event on a keyboard when a user presses enter button wpf 如何在多实例Web用户控件上执行javascript Pageload事件 - How to execute javascript pageload event on multi instance web user control 当用户按下Windows Phone返回键时,如何根据设置页面中的用户输入更新滑块的最大值? - How to update slider's maximum value based on user input in settings page when user presses Windows Phone back key? 执行循环,直到用户按下特定按钮 - Execute a loop until user presses a specific button 当用户按下escape时,如何“暂停”控制台应用程序? - How do I “Pause” a console application when the user presses escape? 当用户按下MessageBox中的按钮时,如何播放声音? - How can I play a sound when a user presses a button in a MessageBox? 使用键盘操作时,仅当用户按下Enter时,才会在列表框上触发SelectedIndexChanged事件 - Fire SelectedIndexChanged event on a listbox, when operating with keyboard, only when user presses Enter 用户关闭浏览器或访问其他页面时如何执行方法? - How to execute a Method when a User close the Browser or visit another Page? 用户按下按钮后如何更新ASP Web应用程序的文字或标签 - How to update ASP Web application Literal or Label after user presses button 当用户按下删除按钮时删除一行 - Deleting a row when the user presses delete button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM