what is the alternate event of Textbox.KeyDown of Winform in WebForm in C# asp.net?

In winfrom applications i can have

textbox1.keydown

event, but i want to achieve same thing in webform (asp.net), so how can i do that? I need to retrieve data from database on this event…

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can use onkeydown event, which will then call your client side function. Inside the client side function, you can make an ajax call to populate data from database.

    <asp:TextBox ID="txtName" runat="server" onkeydown="javascript: callMe();" />

    function callMe()
    {
        $.ajax({
          url: 'URLOFTHEFUNCTION',
          type: 'GET',
          cache: false,
          success: function (result) {
              alert(result)
          }
       });
   }

Method 2

In Win Forms you use running machine and interact with it, as you write smth keydown event is fired, but in web forms, once the client side files are sent from server to multiple clients (for ex. stackoverflow.com and its users) no server side event can be fired without javascript. To achieve your goal you I advice you to catch keyUp event in JS and send data by Callback then return data needed from server to client back and handle that data in JS.

Second Way is to protected void TextBox1_**TextChanged**(object sender, EventArgs e)
use this event, but it will refresh the page on every key up.

Method 3

In web application there are something you do in the client and some in the server.

I don’t now what you what achieve, but i think that in most cases rise the keydown event will be in the client.

SO, there is a great library called JQUERY (javascript) that helps you do this kind of staff.

see the documentation of KEYDOWN

I wrote an example for you

HTML Code

<input id="txtBox" type="text" />​

JQUERY (javascript) Code

$(document).ready(function() {

$("#txtBox").keydown(function() {   
   $.ajax({
   url: "URLTOTHEFUNCTIONINTHESERVER",
   type: 'GET',
   cache: false
   }).done(function( html ) {
    alert(html);
    });
});

});​

OK to explain the code

  • The first line i am checking if the document is ready
  • then i caching the keydown event and firing a function
  • Inside the function i am calling to an ajex function of jquery, inside the function you will see a “done” functio nand a var called html which hold the content that returns from the server


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x