Here is a solution that worked for me with the help of JQuery:
Step 1: SQL Write a Stored Procedure (accepting one input parameter) to Retrieve Paper Names from your database (Assuming you're using SQL Server as a DBMS):
create proc [dbo].[spGetMatchingPaperIds]
@PaperId nvarchar(Whatever size)
as
begin
select PaperId from TableStoringYourPaperIds where PaperId like @PaperId + '%'
end
GO
Step 2: Web Service
Create a web service (and name it PaperSearchService.asmx or whatever naming convention you follow) and write a function that will query your database and retrieve a list of your paper names like so:
[WebMethod]
public List<string> getPaperNames(string DeviceIdString)
{
List<string> paperNames = new List<string>();
string cs = ConfigurationManager.ConnectionStrings["YourConnectionStringToTheDatabase"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spGetMatchingPaperIds", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter parameter = new SqlParameter("@PaperId", PaperId);
cmd.Parameters.Add(parameter);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
paperNames.Add(rdr["PaperId"].ToString());
}
}
return paperNames;
}
Step 3: HTML Markup
I'm assuming you are using a master page so this should go inside one of your content placeholders.
Use developer tools to check for JQuery errors that it may throw on your browser by clicking F12 while debugging and download the relevant JQuery library from the CDN and just include it in your master page but last time I checked its jquery-1.7.min.js
- just double check. This aforementioned library has the autocomplete functionality otherwise it won't work.
<script type="text/javascript">
$(function () {
$('#<%= tbSearchContracts.ClientID %>').autocomplete({
source: function (request, response) {
$.ajax({
url: "PaperSearchService.asmx/getPaperNames",
data: "{ 'PaperId': '" + request.term + "' }",
type: "POST",
dataType: "json",
contentType: "application/json;charset=utf-8",
success: function (data) {
response(data.d);
},
error: function (result) {
alert('There is a problem processing your request');
}
});
},
minLength: 0
});
});
//Below are some nice-to-have's:
function CreateWaterMark(defaultText, textBoxControl) {
if (textBoxControl.value.length == 0) {
textBoxControl.style.color = "gray";
textBoxControl.value = defaultText;
}
}
function ClearWaterMark(defaultText, textBoxControl) {
if (textBoxControl.value == defaultText) {
textBoxControl.style.color = "black";
textBoxControl.value = "";
}
}
</script>
<asp:TextBox ID="tbSearchContracts" CssClass="whatever styling you have used" onblur="CreateWaterMark('Search Paper Names Here', this);" onfocus="ClearWaterMark('Search Paper Names Here', this);" onkeyup="hasPendingChanges()" Text="Search Paper Names Here" runat="server"></asp:TextBox>