Automatic Input Using Web Services
The Web Service is used to automatically complete input. Front-End Interface:
Code
<Asp: textbox id = "textbox1" runat = "server"> </ASP: textbox>
<Ajaxtoolkit: autocompleteextender id = "autocompleteextender3" runat = "server" enabled = "true"
Targetcontrolid = "textbox1"
Servicepath = "/lscomplete. asmx"
Servicemethod = "getcompletedepart"
Completioninterval = "500"
Completionsetcount = "10"
Minimumprefixlength = "1">
</Ajaxtoolkit: autocompleteextender>
Background code:
Create a Web Service:
Code
Using system;
Using system. Web;
Using system. collections;
Using system. Web. Services;
Using system. Web. Services. Protocols;
Using system. Data;
Using system. Data. sqlclient;
Using system. configuration;
/// <Summary>
/// Summary of lscomplete
/// </Summary>
[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
[System. Web. Script. Services. scriptservice]
Public class lscomplete: system. Web. Services. WebService {
Public lscomplete (){
// If you use the designed component, uncomment the following line
// Initializecomponent ();
}
Private Static string [] autocompletewordlist = NULL;
[Webmethod]
Public String [] getcompletedepart (string prefixtext, int count)
{
// If the array is empty
If (autocompletewordlist = NULL)
{
// Read the database content
String connectionstring = configurationsettings. etettings ["sqlconnectionstring"];
Sqlconnection myconn = new sqlconnection (connectionstring );
Myconn. open ();
// Sqldataadapter da = new sqldataadapter ("select region name from departinfo where region name like '" + prefixtext + "% 'order by region name", Conn );
String mysel = "select title from [Table name] Where rtrim (field) like '%" + prefixtext + "% '";
Sqldataadapter adpt = new sqldataadapter (mysel, myconn );
Dataset DS = new dataset ();
Adpt. Fill (DS );
// Read the data of the content file to a temporary Array
String [] temp = new string [Ds. Tables [0]. Rows. Count];
Int I = 0;
Foreach (datarow DR in DS. Tables [0]. Rows)
{
Temp [I] = Dr ["field"]. tostring (). Trim ();
I ++;
}
// Assign the content of the temporary array to the returned array
Autocompletewordlist = temp;
If (myconn. State = connectionstate. open)
Myconn. Close ();
}
String [] returnvalue = new string [count];
Returnvalue = autocompletewordlist;
Autocompletewordlist = NULL;
// Return data
Return returnvalue;
}
}