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?
Asked
Active
Viewed 7,835 times
0

love Computer science
- 1,824
- 4
- 20
- 39
-
1To 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 Jun 21 '11 at 14:39
-
1I haven't ever used ajax yet, but i guess i can learn it for this. – love Computer science Jun 21 '11 at 14:41
3 Answers
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 autocomplete if you're looking to display some information

Chris Cap
- 1,056
- 3
- 13
- 21
2
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.

Jamie Dixon
- 53,019
- 19
- 125
- 162
-3
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.org/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>

Taha Waseem
- 31
- 7

Bibhu
- 4,053
- 4
- 33
- 63