How to create input field dropdown list from database in ASP.NET?

Aleks Mavrič 1 Reputation point
2022-06-07T07:16:12.02+00:00

I'm using ASP.NET 4.8. I'm using the code below to show a dropdown list from database (.aspx file). What do I need to do create an input text field, that shows a dropdown list after the user types something in it? I found a tutorial with AJAX devexpress, but it doesn't run on Visual Studio 2022. The project is shown on a website. Page Language="vb"

<tr>
                                            <td style="font-size:Large;">
                                                Raba tal <b>2018</b>:
                                            </td>
                                            <td>
                                                <asp:DropDownList ID="raba3"  runat="server" DataSourceID="dsSifRaba" DataTextField="mnaziv" DataValueField="raba" SelectedValue='<%# Bind("raba3")%>' OnDataBound="raba3_DataBound" Font-Size="Large" CssClass="ui-widget" ></asp:DropDownList>
                                                <asp:RequiredFieldValidator ID="rfRaba3" runat="server" ErrorMessage="Obvezno polje" ControlToValidate="raba3" CssClass="validator" Display="Dynamic"></asp:RequiredFieldValidator>

                                            </td>
                                        </tr>
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,317 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Lan Huang-MSFT 26,526 Reputation points Microsoft Vendor
    2022-06-07T09:27:08.98+00:00

    Hi @Aleks Mavrič ,
    You can try to download nuget package --AjaxControlToolkit to create input field dropdown.
    209052-image.png

    You can refer to the example below.

    <ajaxToolkit:ComboBox ID="cbCustomers" runat="server" AutoCompleteMode="SuggestAppend">  
    </ajaxToolkit:ComboBox>  
    

    208997-image.png

    Imports System.Data  
    Imports System.Data.SqlClient  
      
    Partial Class _Default  
        Inherits System.Web.UI.Page  
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
            Using con As SqlConnection = New SqlConnection("Data Source=***")  
      
                Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Products", con)  
      
                    Dim sda As SqlDataAdapter = New SqlDataAdapter(cmd)  
                    Dim dt As DataTable = New DataTable()  
                    sda.Fill(dt)  
                    cbCustomers.DataSource = dt  
                    cbCustomers.DataTextField = "ProductName"  
                    cbCustomers.DataValueField = "ProductID"  
                    cbCustomers.DataBind()  
                End Using  
            End Using  
        End Sub  
        Protected Sub Submit(ByVal sender As Object, ByVal e As EventArgs)  
            Dim message As String = "Selected Text: " & cbCustomers.SelectedItem.Text  
            message += "\nSelected Value: " & cbCustomers.SelectedItem.Value  
            ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('" & message & "');", True)  
        End Sub      
    End Class  
    

    208969-123.gif
    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Aleks Mavrič 1 Reputation point
    2022-06-07T13:01:24.577+00:00

    Thank you! Didn't found a solution like that, however I still have a problem.

    I have used the attribute DataValueField="raba"

    I have changed this:

    209120-ergergrgr.png

    To this:
    209152-gergergerger.png

    But I still get an error message.
    209171-jz1456tz4j.png

    Do you have any suggestions on what do I have to fix here?