HERE's an example of an "update-as-you-type" or "onTextChanged" style
textbox control...
This actually posts back to the server on every key stroke, so type slowly, and use with caution!
ASPX SIDE:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>ontextchanged</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="VBScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script type="text/javascript">
function setSelectionRange(input, selectionStart, selectionEnd) {
if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(selectionStart, selectionEnd);
}
else if (input.createTextRange) {
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
}
}
function setCaretToEnd (input) {
setSelectionRange(input, input.value.length, input.value.length);
}
function setCaretToBegin (input) {
setSelectionRange(input, 0, 0);
}
function setCaretToPos (input, pos) {
setSelectionRange(input, pos, pos);
}
function selectString (input, string) {
var match = new RegExp(string, "i").exec(input.value);
if (match) {
setSelectionRange (input, match.index, match.index + match[0].length);
}
}
function replaceSelection (input, replaceString) {
if (input.setSelectionRange) {
var selectionStart = input.selectionStart;
var selectionEnd = input.selectionEnd;
input.value = input.value.substring(0, selectionStart)
+ replaceString
+ input.value.substring(selectionEnd);
if (selectionStart != selectionEnd) // has there been a selection
setSelectionRange(input, selectionStart, selectionStart + replaceString.length);
else // set caret
setCaretToPos(input, selectionStart + replaceString.length);
}
else if (document.selection) {
var range = document.selection.createRange();
if (range.parentElement() == input) {
var isCollapsed = range.text == '';
range.text = replaceString;
if (!isCollapsed) { // there has been a selection
//it appears range.select() should select the newly
//inserted text but that fails with IE
range.moveStart('character', -replaceString.length);
range.select();
}
}
}
}*/
</script>
</HEAD>
<body MS_POSITIONING="FlowLayout">
<form id="Form2" method="post" runat="server">
<asp:TextBox id="Textbox1" runat="server" Width="352px" ></asp:TextBox><BR>
<script>
document.Form1.TxtSearch.focus()
setCaretToEnd(document.Form1.TxtSearch)
</script>
<asp:ListBox id="Listbox1" runat="server" Width="352px" Height="144px"></asp:ListBox>
</form>
</body>
</HTML>
VB SIDE (KEEP IN MIND, The "RecordSet" and "PopulateDropdown" are my own custom classes, you'll have to figure out your own databinding code...:
Public Class ontextchanged
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents TxtSearch As System.Web.UI.WebControls.TextBox
Protected WithEvents LbxSearchResults As System.Web.UI.WebControls.ListBox
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TxtSearch.Attributes.Add("onKeyUp", "this.form.submit()")
GetResults()
End Sub
Private Sub GetResults()
Dim strConn As String = ConfigurationSettings.AppSettings([KEY])
Dim strSQL As String = "SELECT ID, LName FROM [TABLE] WHERE Lname LIKE '" & TxtSearch.Text & "%'"
Dim objDR As System.Data.SqlClient.SqlDataReader
objDR = DataAccess.RecordSet(strSQL, strConn, 2)
DataAccess.PopulateDropDown(objDR, LbxSearchResults, "LName", "ID", "", "", "")
End Sub
End Class
|