Attn: Mark - Dropdownlist solved

B

brian

Mark.

Turns out the problem was in the code below, which is part of dropdown load logic.
When I commented out the line " 'Me.ddlODGroups.DataValueField = "ID"" the problem went away. With the my dataset ID will always be 1 except for the ID that comes from the union. In this example with line not commented out when users selects value and clicks submit the seelcted value gets changed to the first item with a value of 1. When the line is commented out is works correctly.

Same is true if load/bind of control is done in pageload or page_init.

Apparently, duplicate values in the ID is a problem. Don't know if it's a bug or what.


cmd.CommandText = "Select distinct '1' as ID , substring(emWorkGroup,2,len(emWorkGroup)-1) as wGroup " & _
"from ld..tEmployeeMaster " & _
"Where emStatus = '3' and len(emWorkGroup) > 0 " & _
"union " & _
"Select '0' as ID, 'All' as wGroup " & _
"order by ID; "

Me.ddlODGroups.Items.Clear()
Me.ddlODGroups.DataSource = ds.Tables("dsGroups")
'Me.ddlODGroups.DataValueField = "ID"
Me.ddlODGroups.DataTextField = "wGroup"
Me.ddlODGroups.DataBind()


--------------= Posted using GrabIt =----------------
------= Binary Usenet downloading made easy =---------
-= Get GrabIt for free from http://www.shemes.com/ =-
 
N

Norman Yuan

I believe you are talking a dorpdownlist/listbox: its selected item will not
stay after posting back.

Yes, this issue is always there since .NET 1.0: if the ListItems in
dropdownlist/listbox have duplicated value (not text), the selected item of
dropdownlist/listbox may not stay after posting back.

To verify it:

//Add a Listbox/Dropdownlist to the page and populate items with the same
value:

//Add this code into Page_Load(...)

if (!Page.IsPostBack)
{
....
for (int i=0 to 10)
{
ListItem item=new ListItem(i.ToString(),"0"); //Note all items have
the same Value "0"
ListBox1.Items.Add(item)
}
...
}
else
{
//If you degug here, you would see the selectedIndex would be always
"0", no matter what item you selected
int selected=ListBox1.SelectedIndex;
}

Then run the page with a button (or any control) to cause it post back.
You will see no matter what list item you select, after posting back, the
selected item will always be the first item in dropdownlist.listbox.

So, I'd say, the behaviour is "by design".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top