Jazcash Jazcash - 4 years ago 141
ASP.NET (C#) Question

DropDownList Text and Value don't seem right

Got a few weird things going on with my code that I can't figure out:

<asp:DropDownList runat="server" ID="ddlMemberType" OnSelectedIndexChanged="ddlMemberType_OnSelectedIndexChanged" AutoPostBack="True"/>





var memberTypeListItems = memberType.Select(x => new KeyValuePair<string, string>(x.GetValue("Name").ToString(), x.GetValue("LongName").ToString()));
ddlMemberType.DataSource = memberTypeListItems;
ddlMemberType.DataBind();
ddlMemberType.DataTextField = "Value"; // No idea why I need to do this but it gets them the wrong way round otherwise
ddlMemberType.DataValueField = "Key";


This produces the following HTML:

<select name="p$lt$ctl03$pageplaceholder$p$lt$ctl00$pageplaceholder$p$lt$ctl02$BizForm$viewBiz$MembershipCostCalculator$ddlMemberType" onchange="javascript:setTimeout('__doPostBack(\'p$lt$ctl03$pageplaceholder$p$lt$ctl00$pageplaceholder$p$lt$ctl02$BizForm$viewBiz$MembershipCostCalculator$ddlMemberType\',\'\')', 0)" id="p_lt_ctl03_pageplaceholder_p_lt_ctl00_pageplaceholder_p_lt_ctl02_BizForm_viewBiz_MembershipCostCalculator_ddlMemberType">
<option value="Pharmaceutical">Pharmaceutical company (UK based or overseas)</option>
<option value="Consultancy">Consultancy (UK based or overseas)</option>
<option value="Associate">Freelance practitioner (sole trader)</option>
</select>


The problem is with the selected item.
ddlMemberType.SelectedItem
produces:

Attributes: {System.Web.UI.AttributeCollection}
Enabled: true
Selected: true
Text: "[Pharmaceutical, Pharmaceutical company (UK based or overseas)]"
Value: "[Pharmaceutical, Pharmaceutical company (UK based or overseas)]"


The text and value properties are completely off. What's even going on there with the square brackets and comma? Also why are the key/value pairs in the wrong order unless I set the DataTextField to "Value" and the DataValueField to "Key"?

The data in my DB is like so:

Name LongName
=====================
Pharmaceutical Pharmaceutical company (UK based or overseas)
Consultancy Consultancy (UK based or overseas)
Associate Freelance practitioner (sole trader)

Answer Source

You need to swap your code lines around. Assignment of text and value fields should be happening before, not after the data binding:

ddlMemberType.DataSource = memberTypeListItems;
ddlMemberType.DataTextField = "Value";
ddlMemberType.DataValueField = "Key";

ddlMemberType.DataBind();

The way you have it now, when data binding happens drop down list has no idea which field is value, and which one is text, so it is calling ToString on each KeyValuePair for both.

Also, to address your comment in the code: you need to tell DDL how to handle the object it is being fed properly, otherwise the control has no idea which property of the object is "text" one and which is the "value" one. So it tries to guess with some reasonable defaults, and may fail if your use case does not match those defaults.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download