ASP.NET DropDownBox has "Text" for both Text and Value properties
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):
pro开发者_C百科tected 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.
精彩评论