【问题标题】:Handle enter key press event in asp.net在 asp.net 中处理输入按键事件
【发布时间】:2015-08-06 03:31:09
【问题描述】:

我在按钮单击时写了下面的代码行

   protected void btnGo_Click(object sender, EventArgs e)
    {
        try
        {
            SearchHint = txtName.Text.Split('[')[0].ToString().Trim();
            WebUtility.SetCookie(Response, "gslCountry", WebUtility.GetDropDownListValue(ddlCountry, String.Empty), false);
            WebUtility.SetCookie(Response, "gslState", WebUtility.GetDropDownListValue(ddlState, String.Empty), false);
            WebUtility.SetCookie(Response, "gslName", txtName.Text, false);
            WebUtility.SetCookie(Response, "gslCity", txtCity.Text, false);
            BindGrid();
        }
        catch (Exception ex)
        {

        }
    }

现在我希望每当用户按下回车键时,它应该执行与上述代码中的 try 块中相同的操作。请帮助我如何处理asp.net c#中的输入按键事件?

【问题讨论】:

    标签: c# asp.net c#-4.0


    【解决方案1】:

    将您的控件包围在一个 asp:Panel 中并添加 DefaultButton 属性

    <asp:Panel id="pnlSurround" runat="server" DefaultButton="btnGo">
        <asp:Textbox id="textBox1" runat="server" />
        <asp:DropdownList id="dropdownList1" runat="server />
        <asp:Button id="btnGo" runat="server" Text="Go" OnClick="btnGo_Click" />
    </asp:Panel>
    

    只要该面板内的某个控件具有焦点,您就可以按 Enter 执行按钮单击

    【讨论】:

      【解决方案2】:

      ASPX:

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

      JS:

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

      CS:

       protected void btnGo_Click(object sender, EventArgs e)
      {
          try
          {
              SearchHint = txtName.Text.Split('[')[0].ToString().Trim();
              WebUtility.SetCookie(Response, "gslCountry", WebUtility.GetDropDownListValue(ddlCountry, String.Empty), false);
              WebUtility.SetCookie(Response, "gslState", WebUtility.GetDropDownListValue(ddlState, String.Empty), false);
              WebUtility.SetCookie(Response, "gslName", txtName.Text, false);
              WebUtility.SetCookie(Response, "gslCity", txtCity.Text, false);
              BindGrid();
          }
          catch (Exception ex)
          {
      
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多