在TextBox,ASP.NET中处理ENTER按钮
发布时间:2020-12-16 06:58:06 所属栏目:asp.Net 来源:网络整理
导读:我在ASP.NET中遇到以下问题:有一个表单,其中包含一个文本框和一个旁边的按钮,填充框后应该由用户按下( http://www.burnthespam.info上的示例,单击“选择一个”,或者在弹出窗口中使用ReCaptcha时).通常,用户按ENTER键提交表单. 这不会导致按钮上的click事件
我在ASP.NET中遇到以下问题:有一个表单,其中包含一个文本框和一个旁边的按钮,填充框后应该由用户按下(
http://www.burnthespam.info上的示例,单击“选择一个”,或者在弹出窗口中使用ReCaptcha时).通常,用户按ENTER键提交表单.
这不会导致按钮上的click事件被触发,并可能导致意外行为.在burnthespam中,我“尝试”通过检查文本框中是否有数据来解决(但现在如果你做的不同于按下ENTER,就像你按下它一样)来解决它. 你知道是否有另一种方法来使用ENTER键处理表单提交,或者当你按下ENTER时按下我喜欢的按钮的Javascript片段? 编辑 我想在服务器端处理ENTER键事件,即.我已经有了 protected void button_Click(object sender,EventArgs e) { Response.Redirect(...); } 我希望不仅在我使用按钮提交表单时调用该方法(单击或突出显示空格),而且当用户在聚焦文本框时按ENTER键 编辑2 你知道是否有可能以编程方式点击Javascript中的按钮?也许不可能阻止网络钓鱼/垃圾邮件(例如,参见Facebook和“分享给朋友”),但我还是想问… 解决方法
有些方法可以使用表单对象DefaultButton属性或面板的DefaultButton属性设置默认按钮,但我发现它们在过去的各种浏览器中都不可靠,所以通常我依赖于javascript.
此代码的唯一缺点是您必须使用页面指令关闭事件验证,但它应触发单击事件,并触发验证器以及所有这些. 以下是我们使用的代码示例.通常我会将寄存器函数放在实用程序类中,但对于此示例,它位于页面代码中. <%@ Page Language="C#" EnableEventValidation="false" %> <script runat="server"> protected void cmdSubmit1_Click(object sender,EventArgs e) { litValue.Text = "Value 1 - You entered: " + txtValue1.Text; } protected void cmdSubmit2_Click(object sender,EventArgs e) { litValue.Text = "Value 2 - You entered: " + txtValue2.Text; } /// <summary> /// This function registers what button is clicked based on whatever control currently has focus /// so for example if the user password field has focus then you can cause the enter button to click /// if the enter key is pressed This works with ie and firefox as far as I know /// </summary> /// <param name="ControlWithFocus"></param> /// <param name="ControlToClick"></param> private void RegisterDefaultButton(System.Web.UI.Control ControlWithFocus,System.Web.UI.Control ControlToClick) { PostBackOptions p = new PostBackOptions(ControlToClick); p.PerformValidation = true; if (ControlToClick is Button) { p.ValidationGroup = ((Button)ControlToClick).ValidationGroup; } else if (ControlToClick is ImageButton) { p.ValidationGroup = ((ImageButton)ControlToClick).ValidationGroup; } else if (ControlToClick is LinkButton) { p.ValidationGroup = ((LinkButton)ControlToClick).ValidationGroup; } p.RequiresJavaScriptProtocol = false; AttributeCollection a = null; if (ControlWithFocus is HtmlControl) { a = ((System.Web.UI.HtmlControls.HtmlControl)ControlWithFocus).Attributes; } else if (ControlWithFocus is WebControl) { a = ((System.Web.UI.WebControls.WebControl)ControlWithFocus).Attributes; } if (a != null) { a["onKeyDown"] = string.Format("if (event.keyCode == 13) {{{0}}}",ControlToClick.Page.ClientScript.GetPostBackEventReference(p)); } } protected void Page_Load(object sender,EventArgs e) { RegisterDefaultButton(txtValue1,cmdSubmit1); RegisterDefaultButton(txtValue2,cmdSubmit2); } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> </head> <body> <form id="form1" runat="server"> Enter Value 1: <asp:TextBox ID="txtValue1" runat="server"></asp:TextBox> <br /> Enter Value 2: <asp:TextBox ID="txtValue2" runat="server"></asp:TextBox> <br /> <asp:Literal ID="litValue" runat="server"></asp:Literal> <asp:LinkButton ID="cmdSubmit1" runat="server" Visible="false" OnClick="cmdSubmit1_Click">Hidden Button 1</asp:LinkButton> <input id="cmdSubmit2" runat="server" visible="false" type="button" value="Hidden Button 2" onserverclick="cmdSubmit2_Click" /> </form> </body> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.NET Identity 2是否使用machinekey来密码密码?
- asp-classic – ASP页面中的msxml3.dll错误“80072ee2”
- asp.net-mvc – ASP.NET MVC性能
- 如何在asp.net webforms捆绑中将cdN添加到bundle.config
- asp.net-mvc – 如何在ASP.NET MVC视图中对HTML列表项进行分
- asp.net-mvc – MVC 2中自定义模型绑定器的自定义验证属性
- 如何将文件从ASP.NET上载到另一个Web应用程序
- asp.net-mvc – asp.net mvc – 子文件夹
- asp.net-mvc-3 – ASP.NET MVC 3 – 在jquery对话框中编辑动
- 如何测量Live ASP.NET MVC Web应用程序的内存使用情况?
推荐文章
站长推荐
- asp.net-mvc – 是否可以在Visual Studio 2013中
- asp.net-mvc-3 – 在动作过滤器中检查ModelState
- asp.net-mvc – 使用ToString()和@ Html.Display
- asp.net – RegisterStartupScript和执行顺序
- asp.net – 关系从一对多变为多对多需要更新list
- ASP.Net下载大文件的实现方法
- asp.net – MVC4捆绑IncludeDirectory – 文件路
- asp.net – 有人可以帮我理解这个堆栈跟踪吗?
- 需要帮助设置ASP.net 3.5(截图)
- asp.net-core-mvc – 使用asp.net核心的google身
热点阅读