Okay, I feel like I am missing something really simple here. I have an ASP.NET DropDownList control:
<asp:DropDownList ID="rightColumnDropDownList" runat="server"></asp:DropDownList>
In the code behind, I have (simplified, but still has the problem):
protected override void OnPreRender(EventArgs e)
{
ListItemCollection options = new ListItemCollection();
options.Add(new ListItem("name", "value"));
this.rightColumnDropDownList.DataSource = options;
this.rightColumnDropDownList.DataBind();
}
However, the resulting rendered HTML has options that contain the "name" for both the value and the text of the option element.
<option value="name">name</option>
What am I missing here? I also tried this to no avail:
options.Add(new ListItem(){ Text= "name", Value = "value"});
options.Add(new ListItem { Text= "name", Value = "value"});
and then try specifying DataValueField and DataTextField properties:
leftColumnDropDownList.DataValueField = "Value";
leftColumnDropDownList.DataTextField = "Text";
You can use the following code:
dropDownList.Items.Add(new ListItem("text", "value"));
You can also do this:
leftColumnDropDownList.Items.Add("name", "value");
Also, your markup says the id is rightColumnDropDownList
and the code is referencing leftColumnDropDownList
, could just be a mistake when writing the question, though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With