DropDownList - ListItem Value Duplicates

First off; total n00b ASP.NET user. That’ll scare away 75% of the serious people who hate dealing with morons like me.

Second; my problem.

I need a way to pass identical Values from a ListItem.
My reason is that the drop down defines who gets an email.

<asp:DropDownList ID="ddlInfo" runat="server">
<asp:ListItem Value="" Selected="true">Choose One</asp:ListItem>
<asp:ListItem value="Email1">Problem</asp:ListItem>
<asp:ListItem value="Email1">Product</asp:ListItem>
<asp:ListItem value="Email2">Complaint</asp:ListItem>
<asp:ListItem value="Email2">Question</asp:ListItem>
<asp:ListItem value="Email2">Other</asp:ListItem>

How do I pass these values properly?

I have come to realize that these values will pass the initial/first value of the first ListItem if there are duplicates or I can put a single space after the second duplicate, double space after the 3rd duplicate etc. and it will work (although not a proper coding technique).

Many Thanks

I am not sure I understand what you mean.

If you use:
ddlInfo.SelectedValue; you will get the value that is currently selected.

So if the 1st or 2nd Item is selected, you get Email1 and Email2 for any of the rest.

To get the text you can use ddlInfo.SelectedItem.Text to get the text. ie. Problem, Product, etc.

Then do whatever you want with that. What is the problem with duplicate.

You could also just parse the text through. And associate an email address to that?

My problem is if I select something like Product.
I will get Email1 but I get the text Problem instead of Product.

I have zero coding experience so if this is a pain, feel free to forward me to ASP.NET to get an education. I might just pawn it off to a prpgramming group nearby.

You can tell how new I am to programming by my thoughts on your reply. I read your reply and got down to “You could also just parse the text through” and I said, uhm, der, what do you mean.

Hi noob (hehe),

First off, I thought I’ld give a piece of advice. If you were working for me, and wrote an asp.net program which had business logic built into the presentation layer, I would take you out and shoot you (Harsh but I feel fair). To make it clearer, values like Problem, Product or Complaint would be stored in a database, config file or resource file combined with a field/value that states which email they fire. Hence the logic would be nowhere near the asp page. This allows the business logic of the website to be configurable, easy to update and easy to document.


Sometimes you don’t care. You get paid whether you do this job, well or badly. Sometimes the less programming you do, the more time you have for the pub. So why not just put a switch in the code behind, or clientside javascript if thats where it is needed. I would have thought a switch case block would make more sense to you and subsequent programmers, than something obtuse hidden within the <asp : DropDownList>

"When the solution is simple, God is answering. " - Albert Einstein

Well, how are you trying to get the value? Then we can see why you are getting the text instead of the value

I am NOT a programmer, just a pee-on coordinator that’s left holding the bag.
I’ve done my share of HTML, Coldfusion etc. but nothing I would consider ‘programming’. THat’s for smart people like you two.

I agree, I would shoot me too, having the email in the value field is horrible and I’m looking for direction. I would possibly be able to look up info on how to add to the code behind, it’s in C#.

The current configuration is a mess and I’m trying to straighten out as much as I can on a limited budget (ie; no programming staff or web admin).

Where can I learn more about switches and see examples in action. I’m a “Jack of all Trades” so I’m confident I can learn the right way given time.

Thanks for the comments, I don’t think anything thus far was harsh and I consider the current layout ‘Balogne’ (sp?)

NightStalker, it comes from a form and the code behind has this:

protected void btnSubmit_Click(object sender, EventArgs e)
        submitServerSideErrors.Text = "";
        submitServerSideErrors.Visible = false;
        if (serverFormValid("csInquiry"))
            string strQuestion = ddlInfo.SelectedItem.Text;
            string strForwardMail = ddlInfo.SelectedValue;
            string strEmail = email.Text;
            string strAddress = address.Text.Replace("'","''");
            string strCity = city.Text.Replace("'", "''");
            string strState = state.SelectedValue;
            string strZipcode = zipcode.Text;
            string strPhone = phone.Text;

strForwardMail should contain the correct value your looking for. I would not hit the db just for a simple contact us form unless it could expand in the future, but i would rather have if/switch statments set the email address:

if (strQuestion=="Product" || strQuestion=="Problem")
else if (strQuestion=="Complaint" || strQuestion=="Question")

What you can do is something like this:

First, a little data class:

public class EmailTarget
   public string Id {get; set; }
   public string DisplayName {get; set; }
   public string EmailAddress {get; set; }

Second, make a Dictionary of these items in your page’s codebehind so you can bind to it in the UI and search it:

private Dictionary<string, EmailTarget> targets

protected IDictionary<string, EmailTarget> Targets
    get {
       if(targets == null){
            targets = new Dictionary<string, EmailTarget>();
            EmailTarget et = new EmailTarget();
            et.Id = "email1";
            et.DisplayName = "whatever";
            et.EmailAddress = "whatever@example.com";
            // populate the list here.
         return targets;

Third, bind it in your page:

<asp:DropDownList runat="server" ID="EmailTarget" DataTextField="DisplayName" DataValueField="Id" DataSource="Targets" />

Fourth, in the method handling the submit, lookup the address in your dictionary:

EmailTarget et = null;
Targets.TryGetValue(EmailTarget.SelectedValue, out et);
if (et == null)
    throw new ArgumentOutOfRangeException("EmailTarget", "Selected email target does not exist. This is probably a spambot.");
//use the et.EmailAddress field as appropriate.

That should cover most bases for you.


That worked.

I placed your code after I declare the question and forward mail variables.
Then, I took the Value and Text from ListItem in the form and moved text between:

<asp:ListItem>Choose One</asp:ListItem>

Thanks for the assistance, you saved me another 4 hours of frantic searching.


What does this gain me over the ‘Nightstalker’ code?


Nothing, it is just a better way of doing it. With proper OO principals. And its abstracting the code if you need to use it elsewhere. I would have done something similar, but thought it might be a bit over your head if your not a programmer. But if you understand it I would also suggest that method.

oh, and also then the values will not be email addresses in the drop down list

What nightstalker said. That and I was writing my post when he posted his, so there wasn’t a posted solution when I started.

Thanks a lot, I’ll try to get WWB’s methods to work too.
But for now, Nightstalkers works perfectly and was simple to implement.

My brain needs more coffee before I try the WWB method. I can foresee cramping and mumbling in my future.