在ASP.NET的TextBox中处理ENTER按钮 [英] Handling ENTER button in TextBox, ASP.NET
问题描述
我在ASP.NET中存在以下问题:有一个包含文本框和旁边的按钮的表单,应该由用户在填充该框后按下(在http://www.burnthespam.info ,单击选择您的一个",或在弹出窗口中使用ReCaptcha时).通常,用户按ENTER
键提交表单.
I have the following problem in ASP.NET: there is a form that contains a textbox and a button next to it that is supposed to be pressed by the user after filling the box (sample on http://www.burnthespam.info, click "Pick your one", or when using ReCaptcha in a popup). Often, instead, users press ENTER
key to submit the form.
这不会导致按钮上的点击事件被触发,也不会导致意外行为.在Burnthespam中,我试图"通过检查文本框内是否有数据来解决(但是现在,如果您执行的操作与按ENTER
不同,就好像您按了它一样)来解决该问题.
This doesn't cause the click event on the button to be triggered and possibly cause an unexpected behaviour. In burnthespam, I "tried" to solve by checking if there is data inside the text box (but now if you do something different than pressing ENTER
it's like you pressed it) to workaround it.
您是否知道是否还有其他方法可以使用ENTER
键或Java代码段(当您按ENTER
时按下我喜欢的按钮)来处理表单提交?
Do you know if there is another way to handle the form submission with ENTER
key, or a Javascript snippet that when you press ENTER
presses the button I like?
我想处理服务器端上的ENTER键事件,即我已经有
I want to handle the ENTER key event on the server-side, ie. I already have
protected void button_Click(object sender, EventArgs e)
{
Response.Redirect(...);
}
我希望不仅在我使用按钮提交表单(单击或突出显示该空格的表单)时调用该方法,而且还希望在用户聚焦文本框时按下ENTER
时调用该方法
I want that method to be called not only when I submit the form using the button (click or space with it highlighted) but also when a user presses ENTER
when focusing the text-box
您知道是否可以以编程方式单击Javascript中的按钮吗?也许无法阻止网络钓鱼/垃圾邮件(例如,参见Facebook和与朋友共享"),但我仍然想问一下...
Do you know if it's possible to programmatically click a button in Javascript? Maybe it's not possible to prevent phishing/spamming (see Facebook and "share to friends" for example) but I still would like to ask...
推荐答案
有一些方法可以使用表单对象的DefaultButton属性或面板的DefaultButton属性设置默认按钮,但是我发现它们在过去在各种浏览器中使用,所以通常我都依靠javascript.
There some ways you can set a default button using the form object DefaultButton property or the DefaultButton property of a panel, but I have found them to be unreliable in the past in various browsers so usually I rely on javascript.
此代码的唯一缺点是,您必须使用page指令关闭事件验证,但它应该触发点击事件,并触发验证器和所有其他功能.
The only downside to this code is you have to turn off event validation with a page directive, but it should fire off click events, and trigger validators and all that.
这是我们使用的代码示例.通常,我会将register函数放在实用程序类中,但在此示例中,它在页面代码中. 试试看.
Here is an example the code that we use. Normally I would put the register function in a utility class, but for this example it is in the page code. Try this out.
<%@ 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的TextBox中处理ENTER按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!