Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

You can set AutoPostback property true, and call a method.like

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


and Method on code file would be
C#
protected void txtName_TextChanged(object sender, EventArgs e)
   {
       //Data access logic
   }


another way......
The method described below allows you to submit when the user hits the enter key in a TextBox.
XML
<input name="TextBox1" type="text" id="TextBox1" onkeypress="return clickButton(event,'button1')"  />


add javascript
C#
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
 
Share this answer
 
v3
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}; ")
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900