admin管理员组

文章数量:1406152

KeyPress or KeyDown events aren't available in System.Web.UI.WebControls.TextBox so one way to do it is using Java-Scripts, but want to fire some Sql queries at these events. is it possible to execute Sql queries from JavaScript? if not then how do I do it?

KeyPress or KeyDown events aren't available in System.Web.UI.WebControls.TextBox so one way to do it is using Java-Scripts, but want to fire some Sql queries at these events. is it possible to execute Sql queries from JavaScript? if not then how do I do it?

Share Improve this question asked Jun 21, 2011 at 14:35 love Computer sciencelove Computer science 1,8284 gold badges20 silver badges39 bronze badges 2
  • 1 To fire events the page has to be posted and I'm sure you don't want that for every keypress. Would ajax be suitable for this? – tom502 Commented Jun 21, 2011 at 14:39
  • 1 I haven't ever used ajax yet, but i guess i can learn it for this. – love Computer science Commented Jun 21, 2011 at 14:41
Add a ment  | 

3 Answers 3

Reset to default 5

No, You cannot execute SQL from javascript. Your best bet is to use something like jquery and wire up an event to .change() (or something simiiar) and then make an ajax request to perform the sql query. A server side event (which doesn't exist) for textbox key press or key down would submit the page everytime and that just wouldn't work for the user. You might look into jquery ui autoplete if you're looking to display some information

If you need to capture key events, you'll need to use Javascript.

You can use ajax to then send these keys to the server and perform actions.

My guess is that you're thinking of something along the lines of Google Suggest.

You can handle the key press event in the given way

But you can't fire SQL queries in these events.

<%@ Page Language="C#" %>
<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
      Label1.Text = "Start";
   }
   TextBox1.Attributes.Add("onkeyup", "rewriteLabel()");
}
</script>

<script type="text/javascript">

function rewriteLabel()
{
   TextBox1.Text = Label1.text;
}

</script>

<html xmlns="http://www.w3/1999/xhtml" >
<head >
   <title >test</title>
</head>
<body>
   <form id="form1" runat="server">
      <br />
      <asp:TextBox ID="TextBox1" runat="server" /><br />
      <asp:Label ID="Label1" Runat="server" BorderWidth="1px" />
   </form>
</body>
</html>

本文标签: chow do I handle KeyPress or KeyDown events in aspnetStack Overflow