Asp.net DropdownList Va;ues and items

Hi ,
    i am using bellow Dropdown list with values and items ,

 <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem  Value="Red">A1</asp:ListItem>
      <asp:ListItem  Value="green">A2</asp:ListItem>
    <asp:ListItem Value="blue">A3</asp:ListItem>
    <asp:ListItem Value="Red">A4</asp:ListItem>
      <asp:ListItem Value="green">A5</asp:ListItem>
      <asp:ListItem Value="blue">A6</asp:ListItem>
      <asp:ListItem Value="Red">A7</asp:ListItem>      
      <asp:ListItem Value="green">A8</asp:ListItem>      
      <asp:ListItem Value="blue">A9</asp:ListItem>
      <asp:ListItem Value="Red">A10</asp:ListItem>
      <asp:ListItem Value="green">A11</asp:ListItem>
    <asp:ListItem Value="blue">A12</asp:ListItem>
        </asp:DropDownList>

Using this Drop down list ,i retrieving the data at code behind . if i select A12 on dropdown list and i retrieving at  code behind "DropDownList1.SelectedItem.Tosting()" it shows A3 and i select A8 shows A2 and i select  A10 shows A1. I found the reason why it was happening means it selecting the top value of selected value item . Can any one help me how to select respective item with values ..
CPSRIAsked:
Who is Participating?
 
Pratima PharandeCommented:
you need to have unique values for each item
0
 
Ajay SharmaCommented:
To get the right text always use DropDownList1.SelectedItem.SelectedItem.Text

Also are using postabacks to show the value?

If possible post your page's code.
0
 
NavneetSoftware EngineerCommented:
Hi!

Your values are same it has to be unique
else it will take the first encountered values.

Thanks!
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
CPSRIAuthor Commented:
ajaysharmaapjs, here is my page's code -
 protected void DropDownList1_TextChanged1(object sender, EventArgs e)
    {
        string strItem = DropDownList1.SelectedItem.Value.ToString();
        Response.Write(strItem);
    }

pratima_mcs, if i have unique values, it will not work out for me.
0
 
Ajay SharmaCommented:
Any specific reason you are using the _TextChanged event, if not then use DropDownList1_SelectedIndexChanged event
0
 
santhimurthydCommented:
Problem is with your Drop down declaration

<asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem Value="Red">A1</asp:ListItem>
      <asp:ListItem  Value="green">A2</asp:ListItem>
    <asp:ListItem Value="blue">A3</asp:ListItem>
    <asp:ListItem Value="Red">A4</asp:ListItem>
      <asp:ListItem Value="green">A5</asp:ListItem>
      <asp:ListItem Value="blue">A6</asp:ListItem>
      <asp:ListItem Value="Red">A7</asp:ListItem>      
      <asp:ListItem Value="green">A8</asp:ListItem>      
      <asp:ListItem Value="blue">A9</asp:ListItem>
      <asp:ListItem Value="Red">A10</asp:ListItem>
      <asp:ListItem Value="green">A11</asp:ListItem>
    <asp:ListItem Value="blue">A12</asp:ListItem>
        </asp:DropDownList>


when every you select any item in your drop down, the behind it will considered the selected value to identify the item at code behidn and teh first occurs is the answer.
So when you select "A12" the value selected is "Blue"  which is the "A3" value and thus you will get the third item.

To over comegive Unique value for the listitems

this link will help you take a decision
http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/a279e6f5-f0b1-4afc-a53f-22becf608892
0
 
CPSRIAuthor Commented:
Thank you ,  now i am clear about the drop down list.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.