【问题标题】:handling enter press in <asp:textbox处理 <asp:textbox 中的 enter press
【发布时间】:2010-11-17 11:49:52
【问题描述】:

我有一个 asp.net 文本框,我想将其用作搜索框。

我不打算有一个按钮,只是允许用户在文本框中输入他们的搜索关键字并按 Enter。

<div id="search-bar">
    <asp:TextBox ID="txtSearch" runat="server" ></asp:TextBox>
</div>

如何让“输入按下”来调用方法,或在 URL 参数中使用关键字回发页面,例如search.aspx?keywords=this&amp;that?

【问题讨论】:

    标签: asp.net form-submit


    【解决方案1】:

    如果您想调用代码隐藏OnTextChanged 中的函数,请将AutoPostback 设置为true。如果文本框失去焦点(例如 Tab-键)或 Enter-按下键,则会发生这种情况。

    【讨论】:

      【解决方案2】:

      还有其他方法可以使用表单对象 DefaultButton 属性或面板的 DefaultButton 属性设置默认按钮,但我发现它们过去在各种浏览器中不可靠,所以通常我依赖 javascript,如果你不希望按钮可见,您可以将 visible 属性设置为 false。

      此代码的唯一缺点是您必须使用页面指令关闭事件验证,但它应该触发点击事件,并触发验证器等等。

      这是我们使用的代码示例。通常我会将注册函数放在实用程序类中,但对于本示例,它位于页面代码中。

      <%@ 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>
      </html>
      

      【讨论】:

        【解决方案3】:

        用javascript提交表单是:

        document.forms["myform"].submit();
        

        但是 asp 通常会在单击按钮中放置一大堆 javascript 来处理视图状态和类似的事情,因此您最好添加一个设置为表单默认值的按钮,然后将其隐藏CSS。

        【讨论】:

          猜你喜欢
          • 2016-11-06
          • 2015-01-31
          • 1970-01-01
          • 2017-08-14
          • 1970-01-01
          • 1970-01-01
          • 2011-01-26
          • 2010-12-02
          • 1970-01-01
          相关资源
          最近更新 更多