2010-12-04 55 views
5

在ASP.NET中我有以下問題:有一個窗體包含一個文本框和旁邊的一個按鈕,應該在填充框後由用戶按下在http://www.burnthespam.info上點擊「Pick your one」,或在彈出窗口中使用ReCaptcha)。相反,用戶通常會按ENTER鍵提交表單。在TextBox,ASP.NET中處理ENTER按鈕

這不會導致按鈕上的點擊事件被觸發,並可能導致意外的行爲。在burnthespam中,我試着通過檢查文本框內是否有數據來解決問題(但現在如果您按ENTER以外的操作與按下它不同)就可以解決此問題。

你知道是否有另一種方式來處理表格提交ENTER鍵或一個JavaScript片段,當你按ENTER按下我喜歡的按鈕?

編輯

我要處理的服務器端,即ENTER鍵事件。我已經有

protected void button_Click(object sender, EventArgs e) 
    { 
     Response.Redirect(...); 
    } 

我想不僅要調用的方法,當我使用按鈕提交表單(點擊或與它的空間高亮顯示),而且當用戶對焦時的文本框

按下 ENTER

編輯2

你知道嗎?以編程的方式點擊Javascript中的按鈕?也許這是不可能的,以防止網絡釣魚/垃圾郵件(見Facebook和「分享給朋友」例如),但我仍然想問...

回答

0

有一些方法可以使用表單對象DefaultButton屬性設置默認按鈕或面板的DefaultButton屬性,但我發現它們在過去的各種瀏覽器中都不可靠,所以通常我依賴於javascript。

這段代碼唯一的缺點是你必須關閉頁面指令的事件驗證,但它應該觸發點擊事件,並觸發驗證器和所有。

以下是我們使用的代碼示例。通常我會將註冊函數放在一個實用程序類中,但在本例中它是在頁面代碼中。 試試這個。與在渲染的在標記您的按鈕,即ClientID屬性的ID

<html> 
<head> 
<script type="text/javascript"> 
function test(e){ 
var keynum; 

if(window.event) // IE 
{ 
keynum = e.keyCode 
} 
else if(e.which) // Netscape/Firefox/Opera 
{ 
keynum = e.which 
} 
if(keynum==13) __doPostback('yourButtonId', ''); 
} 
</script> 
</head> 
<body> 
<input type="text" onkeypress="test(event)" /> 
</body> 
</html> 

你需要更換「yourButtonId」:

<%@ 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> 
+0

我剛剛注意到,你的代碼很少修改,只是IE8在IGNORED中工作。難道這是由於TextBox是一個gaia:來自Gaia AJAX WebWidgets的TextBox,並且僅在回發後纔出現?我仔細檢查了生成的HTML匹配按鈕單擊,並且我甚至不必禁用事件驗證(因爲在FF作品中),但我仍然得到一個空HTTP(不是AJAX像FF)回發 – 2011-01-30 02:08:25

2

你可以試試這個。 NET代碼。 __doPostback函數是由.NET定義的用於處理其所有回發的函數。

3

下面是ASP.NET更簡單的方法偉大工程:

在.aspx頁面

<script type="text/javascript"> 
     function clickButton(e, buttonid) { 
      var evt = e ? e : window.event; 
      var bt = document.getElementById(buttonid); 
      if (bt) { 
       if (evt.keyCode == 13) { 
        bt.click(); 
        return false; 
       } 
      } 
     } 
</script> 

添加這一點,添加這個在您的ASPX在Page_Load。cs文件

textbox1.Attributes.Add("onkeypress", "return clickButton(event,'" + buttonName1.ClientID + "')");