Auto Complete Box in WPF Application
Design a page like below
<Label Content="Account : " FontWeight="Normal" Height="25" Margin="22,21,237,216" Name="Label6" />
<my:AutoCompleteBox Grid.Column="0" HorizontalAlignment="Left" Margin="98,20,0,0" MinimumPrefixLength="2" FilterMode="Contains" Name="txtLsname" VerticalAlignment="Top" Width="144" Height="25" ToolTip="Pick code" IsTabStop="True" />
Write the below code in Code behind
Private NameList As New List(Of String)()
Try
NameList = objReadData.GetCompletionListACC("", 0, "")
txtLsname.ItemsSource = NameList
Catch ex As Exception
System.Windows.MessageBox.Show("Could Not get AutoCompletion Details.")
End Try
Public Function GetCompletionListACC(ByVal prefixText As String, ByVal count As Integer, ByVal contextKey1 As String) As Object
Try
SqlCommand command = new SqlCommand("SELECT AccountID, AccountName FROM Account;",connection);
If _dr.HasRows = True Then
While _dr.Read
c1 = _dr("AccountName").ToString.ToUpper.Trim
items.Add(c1)
End While
End If
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error")
End Try
Return items
End Function
Auto Complete Box in web Application (ASP.NET)
<my:AutoCompleteBox Grid.Column="0" HorizontalAlignment="Left" Margin="98,20,0,0" MinimumPrefixLength="2" FilterMode="Contains" Name="txtLsname" VerticalAlignment="Top" Width="144" Height="25" ToolTip="Pick code" IsTabStop="True" />
Write the below code in Code behind
Private NameList As New List(Of String)()
Try
NameList = objReadData.GetCompletionListACC("", 0, "")
txtLsname.ItemsSource = NameList
Catch ex As Exception
System.Windows.MessageBox.Show("Could Not get AutoCompletion Details.")
End Try
Public Function GetCompletionListACC(ByVal prefixText As String, ByVal count As Integer, ByVal contextKey1 As String) As Object
Try
SqlCommand command = new SqlCommand("SELECT AccountID, AccountName FROM Account;",connection);
SqlDataReader _dr= command.ExecuteReader();
If IsDBNull(_dr) = False ThenIf _dr.HasRows = True Then
While _dr.Read
c1 = _dr("AccountName").ToString.ToUpper.Trim
items.Add(c1)
End While
End If
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error")
End Try
Return items
End Function
Auto Complete Box in web Application (ASP.NET)
Design a page like below
<td>
<asp:Label ID="lblPartNo" runat="server" Text="Code" /> </td>
<td>
<div>
<asp:TextBox ID="txtPartno" runat="server" CssClass="txtbx" Columns="20" MaxLength="7"
Style="text-transform: uppercase" AutoPostBack="True" ToolTip="Enter the Part#"></asp:TextBox>
<asp:AutoCompleteExtender runat="server" ID="autoComplete" TargetControlID="txtPartno" ServicePath="../CustomerAdminUser/AutoComplete.asmx" ServiceMethod="GetPartDetails"
MinimumPrefixLength="2" CompletionInterval="10" EnableCaching="true" ContextKey="True"
CompletionSetCount="5" CompletionListCssClass="AutoExtender" CompletionListItemCssClass="AutoExtenderList"
CompletionListHighlightedItemCssClass="AutoExtenderHighlight" /> <span class="fontred">*</span>
</div>
</td>
Create a service method for Auto complete Box
<WebMethod(EnableSession:=True)> _
Public Function GetPartDetails(ByVal prefixText As String, ByVal count As Integer, ByVal contextKey As String) As String()
Dim items As New List(Of String)
SqlConnection connection= new SqlConnection (";")
SqlCommand command = new SqlCommand("SELECT AccountID, AccountName FROM Account;",connection);
SqlDataReader _dr = command.ExecuteReader();
Try
If IsDBNull(_dr) = False Then
If _dr.HasRows = True Then
While _dr.Read
items.Add(_dr("AccountName ").ToString.ToUpper.Trim)
End While
End If
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error")
End Try
If connection.State = ConnectionState.Open Then connection.Close()
Return items.ToArray()
End Function
<asp:Label ID="lblPartNo" runat="server" Text="Code" /> </td>
<td>
<div>
<asp:TextBox ID="txtPartno" runat="server" CssClass="txtbx" Columns="20" MaxLength="7"
Style="text-transform: uppercase" AutoPostBack="True" ToolTip="Enter the Part#"></asp:TextBox>
<asp:AutoCompleteExtender runat="server" ID="autoComplete" TargetControlID="txtPartno" ServicePath="../CustomerAdminUser/AutoComplete.asmx" ServiceMethod="GetPartDetails"
MinimumPrefixLength="2" CompletionInterval="10" EnableCaching="true" ContextKey="True"
CompletionSetCount="5" CompletionListCssClass="AutoExtender" CompletionListItemCssClass="AutoExtenderList"
CompletionListHighlightedItemCssClass="AutoExtenderHighlight" /> <span class="fontred">*</span>
</div>
</td>
Create a service method for Auto complete Box
<WebMethod(EnableSession:=True)> _
Public Function GetPartDetails(ByVal prefixText As String, ByVal count As Integer, ByVal contextKey As String) As String()
Dim items As New List(Of String)
SqlConnection connection= new SqlConnection (";")
SqlCommand command = new SqlCommand("SELECT AccountID, AccountName FROM Account;",connection);
SqlDataReader _dr = command.ExecuteReader();
Try
If IsDBNull(_dr) = False Then
If _dr.HasRows = True Then
While _dr.Read
items.Add(_dr("AccountName ").ToString.ToUpper.Trim)
End While
End If
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error")
End Try
If connection.State = ConnectionState.Open Then connection.Close()
Return items.ToArray()
End Function