ASP.NET DropDownBox has β€œText” for text properties and value

Well, I feel like I'm missing something really simple here. I have an ASP.NET DropDownList control:

<asp:DropDownList ID="rightColumnDropDownList" runat="server"></asp:DropDownList> 

In the code behind me (simplified, but still have a 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 HTML code has parameters that contain a "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"}); 
+4
source share
3 answers
 options.Add(new ListItem { Text= "name", Value = "value"}); 

and then try specifying the DataValueField and DataTextField properties:

 leftColumnDropDownList.DataValueField = "Value"; leftColumnDropDownList.DataTextField = "Text"; 
+8
source

You can use the following code:

 dropDownList.Items.Add(new ListItem("text", "value")); 
+3
source

You can also do this: leftColumnDropDownList.Items.Add("name", "value");

In addition, your markup says that the identifier is rightColumnDropDownList , and the code refers to leftColumnDropDownList , there may be a mistake when writing the question.

+1
source

Source: https://habr.com/ru/post/1347636/


All Articles