March 6, 2017 at 3:59 pm
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>
March 7, 2017 at 4:10 am
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.
March 19, 2017 at 9:03 pm
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 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy