Drop Down in Visual Studio .Net

  • Hello, I am not an app developer but need to create a web page in a pinch.  I am looking to create a drop down menu that pulls the current value from a database table and then hard code 2 other options.  I can pull the value from the database table but cannot seem to hard code additional values.  Here is my code, can someone help me understand what I am doing wrong?  Currently this will print the hard coded values to the right of the drop down.

    Code:

    <asp:TemplateField HeaderText="DropDown">
            <ItemTemplate>
                <%#Eval("My_DropDown")%>
            </ItemTemplate>

            <EditItemTemplate>
                <asp:DropDownList id="My_DropDown" datasourceid="ds"
                datatextfield="My_DropDown"     DataValueField="My_DropDown"
                SelectedValue='<%# Bind("My_DropDown") %>' runat="server"/>
    <asp:ListItem>Drop Down Option 1</asp:ListItem>
    <asp:ListItem>Drop Down Option 2</asp:ListItem>
         </asp:DropDownList>
       </EditItemTemplate>

  • If what you're looking for is to programmatically add items to the dropdown list (and I think it is) this would do the trick:


    protected void AddItem(object sender, EventArgs e)
    {
      string fruit = txtFruit.Text.Trim();
      if (!string.IsNullOrEmpty(fruit))
      {
       ddlFruits.Items.Add(new ListItem(fruit, fruit));
      }
    }

    Additionally have a look into this reply on SO.


    My stack: VS2015 ~ C# ~ MSSQL 2014[/url] ~ DevCraft ~ ReSharper ~ DataGrip ~ Linqer[/url]

  • Hi,
    DataTextField and DataValue field both are different.
    suppose you have table in the below format

    Id  supplier_name
    1   Kodak
    2   Hp
    3   Epson


    Then the "DataTextField" is "supplier_name" and "DataValueField" is "Id". Based on Id it display the supplier name in the dropdown list.
    Hope this will be useful.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply