0

In WinForms applications, I can have a textbox1.keydown event, but I want to achieve the same thing in a WebForm (ASP.NET), so how can I do that?

I need to retrieve data from the database on this event.

fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88
Drone
  • 181
  • 8
  • 22

3 Answers3

2

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)
          }
       });
   }
Lews Therin
  • 10,907
  • 4
  • 48
  • 72
Developer
  • 759
  • 2
  • 9
  • 24
  • i am new to asp.net so the function callMe will be written in a .js file?? – Drone Sep 24 '12 at 07:28
  • yes, it can be. Or else put it inside a script tag within the aspx page like – Developer Sep 24 '12 at 07:29
  • do i have to include any namespace for this? bcoz i am not getting the `onkeydown` – Drone Sep 24 '12 at 07:35
  • No need to use any namespaces.. Just write it the way i have suggested, and try putting an alert inside the function to check if the function is getting called or not.. – Developer Sep 24 '12 at 07:38
  • I want a suggestion from u. Instead of `` can I write `Textbox1.Attributes.Add("onkeydown", "callMe")` on page_load .. will this work ?? – Drone Sep 24 '12 at 07:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/17051/discussion-between-nick-and-drone) – Developer Sep 24 '12 at 08:22
0

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.

levi
  • 3,451
  • 6
  • 50
  • 86
  • ok thanks but as u said it will refresh each time so that might be problem for the user .. but thanks i will try this one also – Drone Sep 24 '12 at 07:42
  • Yes it's very bad. So, as I wrote use the first solution. – levi Sep 24 '12 at 07:48
0

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
Silagy
  • 3,053
  • 2
  • 27
  • 39
  • actually for now i don't know much about jquery so i am kind of hesitate to use that. – Drone Sep 24 '12 at 07:49
  • I understand, but i think when you decide to do so. your life will be easier. I am taking from self experience. – Silagy Sep 24 '12 at 07:51