按Enter键获取数据 [英] fetch data on pressing enter key

查看:76
本文介绍了按Enter键获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户按Enter键将代码放入文本框中后,从数据库(sqlserver)中获取数据?在桌面应用程序上很常见,
使用失去焦点的事件!但是我们如何使用c#
在asp.net中做到这一点?
Help

i want to fetch data from database(sqlserver) after putting code in text box as user press enter key ?? its quite common on desktop application to
use lost focus event !! but how would we do it in asp.net with c#

Help

推荐答案

您可以将AutoPostback属性设置为true,然后调用一个方法.
You can set AutoPostback property true, and call a method.like

<asp:TextBox ID="txtName" runat="server" Width="184px" AutoPostBack="True" ontextchanged=" txtName_TextChanged"></asp:TextBox>



和代码文件上的方法为



and Method on code file would be

protected void txtName_TextChanged(object sender, EventArgs e)
   {
       //Data access logic
   }



另一种方式...
当用户在TextBox中按下Enter键时,下面描述的方法允许您提交.



another way......
The method described below allows you to submit when the user hits the enter key in a TextBox.

<input name="TextBox1" type="text" id="TextBox1" onkeypress="return clickButton(event,'button1')"  />



添加javascript



add javascript

function clickButton(e, button1){
      var evt = e ? e : window.event;
      var bt = document.getElementById(button1);
      if (bt){
          if (evt.keyCode == 13){
                bt.click();
                return false;
          }
      }
}



希望这对您有帮助



Hope this helps


您需要在文本框中添加一个自定义属性,类似于以下内容:(我的是VB.NET,但您可以整理出C#的语法)

You need to add a custom attribute to your text box, similar to this: (Mine is VB.NET, but you can sort out the syntax for C#)

txt_TEXTBOX_NAME.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById(''" + cmd_BUTTON_YOU_WANT_TO_FIRE.UniqueID + "'').click();return false;}} else {return true}; ")


这篇关于按Enter键获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆