Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET DropDownBox has "Text" for both Text and Value properties

Tags:

asp.net

c#-4.0

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"});
like image 301
6of58 Avatar asked Apr 11 '11 21:04

6of58


Video Answer


3 Answers

options.Add(new ListItem { Text= "name", Value = "value"});

and then try specifying DataValueField and DataTextField properties:

leftColumnDropDownList.DataValueField = "Value";
leftColumnDropDownList.DataTextField = "Text";
like image 137
Darin Dimitrov Avatar answered Oct 11 '22 00:10

Darin Dimitrov


You can use the following code:

dropDownList.Items.Add(new ListItem("text", "value"));
like image 4
Akram Shahda Avatar answered Oct 10 '22 22:10

Akram Shahda


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.

like image 1
Rex Morgan Avatar answered Oct 10 '22 22:10

Rex Morgan