Aha, that explains it. Setting .Selected mark the item as selected. And if
the same item belongs to 2 ddls, the second one gets find itself having two
selected items since noone unselected the previous one.
Thanks for a nice excercise.
Eliyahu
Okay..... that worked! But I'm not sure why...
I changed the code that filled the DDLs so that it created 2 ListItem
thingies, one called iSThisHour and iEThisHour. It worked great.
So... why does using the same ListItem with 2 DDLs cause a SelectedIndex =
on one of the DDLs to change the SelectedIndex on the other DDL?
Anyone enlighten me?
:
Hi Eliyahu,
Right, I've changed all the .Selected to SelectedIndex and that seems to
have got rid of the Multiple Select error thing.
Stil lhaving trouble with the duplication though.. I've renamed all the
DDLs, moved things around, etc.
The only thing I can think of is that the peice of code that fills the
DDLs
with the hours (09, 10, 11, 12, 13 etc) fills both boxes at the same
time
using the same ListItem. eg...
ListItem iThisHour = new ListItem();
iThisHour.Text = curHour.ToString();
iThisHour.Value = curHour.ToString();
ddlStartTimeHour.Items.Add(iThisHour);
ddlEndTimeHour.Items.Add(iThisHour);
iThisHour = null;
Where curHour is the for loop counter.
Could this be anything to do with it? Having looked at the code again,
it
appears to be the only place where the 2 DDLs meet, as it were.
Thoughts?
Cheers
Dan
:
Dan,
I am not sure that .SelectedIndex and .Selected is the same. ..Selected
is a
property of just one item. It has no way of knowing what's going on
with the
other items. Whereas .SelectedIndex is a property of ddl, and the ddl
can
first unselect previously selected item.
As far as your weird problem is concerned, it is very weird indeed.
Check
carefully the aspx, is there any way the ddls can refer to the same
object?
Eliyahu
Hi Eliyahu,
Thanks for the reply. Unfortunately that's even weirder.
I understand the theory of the DDL, and I assume that
Items.FindByValue
gives u the index number, so doing .Selected = true is the same as
.SelectedIndex = x;
However, I have tried it and got some very strange behaviour...
Here's my
previous post on this topic, posted yesterday .....
-------
OK, I'm getting a weird error trying to select a DDL item based on
what's
stored in the database.
I have 4 DDLs on my aspx page...
<asp:dropdownlist id="ddlStartTimeHour" runat="server" />
<asp:dropdownlist id="ddlEndTimeHour" runat="server" />
<asp:dropdownlist id="ddlStartTimeMinute" runat="server" />
<asp:dropdownlist id="ddlEndTimeMinute" runat="server" />
these are declared in the C# as
protected System.Web.UI.WebControls.DropDownList ddlStartTimeHour;
protected System.Web.UI.WebControls.DropDownList ddlStartTimeMinute;
protected System.Web.UI.WebControls.DropDownList ddlEndTimeHour;
protected System.Web.UI.WebControls.DropDownList ddlEndTimeMinute;
They are populated on Page_load with a couple of for loops (09, 10,
11 etc
for hour, 00,15,30,45 for minute). Then, Im trying to select the
correct
data
based on a DataReader as follows..
string[] timeStart, timeEnd;
timeStart = dr["SessionTimeStart"].ToString().Split(':');
timeEnd = dr["SessionTimeEnd"].ToString().Split(':');
ddlStartTimeHour.Items.FindByValue(timeStart[0]).Selected = true;
ddlStartTimeMinute.Items.FindByValue(timeStart[1]).Selected = true;
ddlEndTimeHour.Items.FindByValue(timeEnd[0]).Selected = true;
ddlEndTimeMinute.Items.FindByValue(timeEnd[1]).Selected = true;
The first two (the Start times) seem to work fine, but I get an
error on
the
EndTimeHour that says "A DropDownList cannot have multiple items
selected". I
dont understand why this is happening, it happens even if I add
ddlEndTimeHour.SelectedItem = -1.
Even odder than that... the StartTime for the test date is 14:00,
and the
end time is 15:30. Obviously theStartTIme works, but if I comment
out the
2
EndTime.Select lines, the EndTime DDLs shows 14:00 as well!
It's as if ddlStartTimeHour and ddlEndTimeHour are connected, hence
I get
the error about no multiples. But that *cant* be the case... can
it?!
------------
Having tried your idea of using SelectedIndex, what apepars to have
happened
is that BOTH ddlStartTimeHour and ddlEndTimeHour have taken the
value
given
in the SelectedIndex statement...
ddlEndTimeHour.SelectedIndex = 3;
I don't understand why these 2 DropDownLists appear connected ?!!?
Please help! This is driving me crazy!!
(
Dan
:
Dan,
A ddl has always at least one item selected unless it is empty.
You
should
set SelectedItem or SelectedIndex properties to change selection.
Eliyahu
Hi guys
Still having a problem with this dropdownlist.
Basically, I've got 4. The first 2 work fine, then my code
crashes on
the
3rd.
ddlEndTimeHour.Items.FindByValue(endTime[0]).Selected = true;
Where endTime[0] is a string containing "15".
I get the error "A DropDownList cannot have multiple items
selected".
I
know
I'm not selecting any items anywhere else, although when I look
at the
generated HTML, the first item is marked selected="selected".
I've
tried
adding
ddlEndTimeHour.Items[0].Selected = false;
but that hasnt helped.
What's going on?!
Cheers
Dan