Address book data lookup (maybe javascript)

R

Radu

Hi. I have the following problem - I need to build a user-control in
asp.net (an ascx) to somehow allow the users to search by first name
or last name among a big (~10.000 records) email addresses database
(SQL server table). I am thinking of allowing them to type something
in EITHER the last name, EITHER the first name textbox, and on each
text change to show in a combobox the records starting with that
letter combination.

Say for example that you have the following email addresses:

Hardy
John Doe
John Foe
John Poe
Johny Roe
Johny Zoe
Laurel
......

When you type in the FirstName textbox the string "John", the combo
box should position itself on John Doe. Hardy should still be visible
right above John Doe, if you open the combobox, but John Doe should be
selected. If you type Johny, it should position itself onto Johny Roe.
If you type Johnson, it should show the first element of the combobox,
a null string, since there is no Johnson in the list.

Similar functionality for the LastName textbox.

Finally, when you are happy with the selection in the combobox, say
"Johny Roe", you click a 'Search' button which will populate the
search form with Johny Roe's ID.

My problem is that I don't know how to best implement this. I'm
thinking of tying somehow keypresses with some server code which will
interogate the SQL server database. I'm afraid of the reaction time of
this control, though.

Question: How do I associate code with keypresses ? I can't use
Javascript for this, because I need to look up the string into SQL
tables on the server, and a textbox has only a TextChanged event.

Although I know how to write the server code, I still don't know how
to do this overall - and still I have seen something like that before,
and it worked great, and super-fast - actually I don't know if it
involved trips to the server, although I guess it did.

Do you have any advice, please ? Thank you very much !
Alex. Nitulescu
 
W

wmain

Hi. I have the following problem - I need to build a user-control in
asp.net (an ascx) to somehow allow the users to search by first name
or last name among a big (~10.000 records) email addresses database
(SQL server table). I am thinking of allowing them to type something
in EITHER the last name, EITHER the first name textbox, and on each
text change to show in a combobox the records starting with that
letter combination.

Say for example that you have the following email addresses:

Hardy
John Doe
John Foe
John Poe
Johny Roe
Johny Zoe
Laurel
.....

When you type in the FirstName textbox the string "John", the combo
box should position itself on John Doe. Hardy should still be visible
right above John Doe, if you open the combobox, but John Doe should be
selected. If you type Johny, it should position itself onto Johny Roe.
If you type Johnson, it should show the first element of the combobox,
a null string, since there is no Johnson in the list.

Similar functionality for the LastName textbox.

Finally, when you are happy with the selection in the combobox, say
"Johny Roe", you click a 'Search' button which will populate the
search form with Johny Roe's ID.

My problem is that I don't know how to best implement this. I'm
thinking of tying somehow keypresses with some server code which will
interogate the SQL server database. I'm afraid of the reaction time of
this control, though.

Question: How do I associate code with keypresses ? I can't use
Javascript for this, because I need to look up the string into SQL
tables on the server, and a textbox has only a TextChanged event.

Although I know how to write the server code, I still don't know how
to do this overall - and still I have seen something like that before,
and it worked great, and super-fast - actually I don't know if it
involved trips to the server, although I guess it did.

Do you have any advice, please ? Thank you very much !
Alex. Nitulescu

In the TextChanged event of the text box you can perform a lookup of
the first value containing the StartsWith(txtFirsName.Text) within the
dropdownlists list of values. The first found item I'd set the
dorpdownlist's selected index value to that entry's index.

foreach(x=0; x<dropdownlist.items.count; x++)
if (dropdownlist.items[x].text.startswith(txtfirstname.text))
{
dropdownlist.selectedindex=x;
break;
}

This begs for the use of the Ajax Toolkit. You could do the lookup and
refresh the dropdownlist in the background using an asynchronous
callback by placing the controls on an Ajax update panel.
 
W

wmain

Hi. I have the following problem - I need to build a user-control in
asp.net (an ascx) to somehow allow the users to search by first name
or last name among a big (~10.000 records) email addresses database
(SQL server table). I am thinking of allowing them to type something
in EITHER the last name, EITHER the first name textbox, and on each
text change to show in a combobox the records starting with that
letter combination.
Say for example that you have the following email addresses:
Hardy
John Doe
John Foe
John Poe
Johny Roe
Johny Zoe
Laurel
.....
When you type in the FirstName textbox the string "John", the combo
box should position itself on John Doe. Hardy should still be visible
right above John Doe, if you open the combobox, but John Doe should be
selected. If you type Johny, it should position itself onto Johny Roe.
If you type Johnson, it should show the first element of the combobox,
a null string, since there is no Johnson in the list.
Similar functionality for the LastName textbox.
Finally, when you are happy with the selection in the combobox, say
"Johny Roe", you click a 'Search' button which will populate the
search form with Johny Roe's ID.
My problem is that I don't know how to best implement this. I'm
thinking of tying somehow keypresses with some server code which will
interogate the SQL server database. I'm afraid of the reaction time of
this control, though.
Question: How do I associate code with keypresses ? I can't use
Javascript for this, because I need to look up the string into SQL
tables on the server, and a textbox has only a TextChanged event.
Although I know how to write the server code, I still don't know how
to do this overall - and still I have seen something like that before,
and it worked great, and super-fast - actually I don't know if it
involved trips to the server, although I guess it did.
Do you have any advice, please ? Thank you very much !
Alex. Nitulescu

In the TextChanged event of the text box you can perform a lookup of
the first value containing the StartsWith(txtFirsName.Text) within the
dropdownlists list of values. The first found item I'd set the
dorpdownlist's selected index value to that entry's index.

foreach(x=0; x<dropdownlist.items.count; x++)
if (dropdownlist.items[x].text.startswith(txtfirstname.text))
{
dropdownlist.selectedindex=x;
break;
}

This begs for the use of the Ajax Toolkit. You could do the lookup and
refresh the dropdownlist in the background using an asynchronous
callback by placing the controls on an Ajax update panel.

I should mention that using a dictionary of the text value and the
item's position in the list might speed up the search. What you'd have
to do is create a dictionary of every possible string as the key and
set the value to the index of the first item in the dropdownlist that
starts with that string value. By the size of your list (10,000+) you
might even want to create an index in sql server and use a sql query
to do this. Keeping this index up to date would require an insert/
update trigger in the database table containing the list of addresses.
This could potentially slow down inserts and updates to that table in
the database.

The trigger could be similar to

select top 1 Id
from addreses
where substring(firstname,1,1] = @firstcharacter
order by descending

Another Idea would be to use a hybrid approach. You could create a
dictionary with the value being the first character of the name and
the key being the first index in the list beginning with that
character. You could then start a linear search using the StartsWith
method from that index forward until the first character changes. This
would limit the number of items you'd have compare to in the list for
any given string. Again, an sql index might speed the creation of this
dictionary with the same caveats as above.
 
R

Radu

Thank you, "WMAIN" ;-) Great ideas, I will start working on them/
thinking about it 1st thing tomorrow morning.

However, I still have a question/problem:

How could one NOT use TextChanged (this assumes hitting ENTER every
time you want to search - really incovenient !) and use a KeyPress
type of event instead ? Is it possibile ? A way would be, I think, to
download the whole list locally (just as I do, actually, when
populating the combo-box (I'm using a sproc)), and search there, in
some array/collectiion/special collection, maybe.... what do you
think ? That would not involve any trip to the server - the only thing
is that I know VB/VBScript, not JavaScript (I know NO JavaScript at
all), to write this kind of code. But I could try, nevertheless.

Thanks a lot ! :)))
Alex.


In the TextChanged event of the text box you can perform a lookup of
the first value containing the StartsWith(txtFirsName.Text) within the
dropdownlists list of values. The first found item I'd set the
dorpdownlist's selected index value to that entry's index.
foreach(x=0; x<dropdownlist.items.count; x++)
if (dropdownlist.items[x].text.startswith(txtfirstname.text))
{
dropdownlist.selectedindex=x;
break;
}
This begs for the use of the Ajax Toolkit. You could do the lookup and
refresh the dropdownlist in the background using an asynchronous
callback by placing the controls on an Ajax update panel.

I should mention that using a dictionary of the text value and the
item's position in the list might speed up the search. What you'd have
to do is create a dictionary of every possible string as the key and
set the value to the index of the first item in the dropdownlist that
starts with that string value. By the size of your list (10,000+) you
might even want to create an index in sql server and use a sql query
to do this. Keeping this index up to date would require an insert/
update trigger in the database table containing the list of addresses.
This could potentially slow down inserts and updates to that table in
the database.

The trigger could be similar to

select top 1 Id
from addreses
where substring(firstname,1,1] = @firstcharacter
order by descending

Another Idea would be to use a hybrid approach. You could create a
dictionary with the value being the first character of the name and
the key being the first index in the list beginning with that
character. You could then start a linear search using the StartsWith
method from that index forward until the first character changes. This
would limit the number of items you'd have compare to in the list for
any given string. Again, an sql index might speed the creation of this
dictionary with the same caveats as above.- Hide quoted text -

- Show quoted text -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top