Shanna
Shanna

Reputation: 783

ListBox1.SelectedItem.Text gives other Text

I have a listbox :

<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True"OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
<asp:ListItem Value="genyn">MAS Meeting</asp:ListItem>   
<asp:ListItem Value="smartyn">Smart Meeting</asp:ListItem>  
<asp:ListItem Value="genyn">Project Meeting</asp:ListItem>
</asp:ListBox>

In the code behind:

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
 label1.Text= ListBox1.SelectedItem.Text;
}

when I select Project Meeting in the ListBox, label1 has the text "MAS Meeting". But i want it hav Project Meeting. Is it because I have same value for first and third listitem?

Can anyone help me? Thanks in advance

Upvotes: 0

Views: 164

Answers (2)

Murali Murugesan
Murali Murugesan

Reputation: 22619

You could not do it such a way.

The value attribute should be unique, otherwise you will get the first matched item from the select, mean asp.net List

<asp:ListItem Value="diffValue">Project Meeting</asp:ListItem>

Upvotes: 1

Andrei
Andrei

Reputation: 56688

You have the same values for MAS Meeting and Project Meeting - that can cause your problem. Try setting different values for different list items to avoid confusion:

<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True"OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
    <asp:ListItem Value="genyn">MAS Meeting</asp:ListItem>   
    <asp:ListItem Value="smartyn">Smart Meeting</asp:ListItem>  
    <asp:ListItem Value="another_genyn">Project Meeting</asp:ListItem>
</asp:ListBox>

Upvotes: 1

Related Questions