By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,053 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,053 IT Pros & Developers. It's quick & easy.

DropDownList.FindByValue.Selected not working... help!

P: n/a
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
Nov 19 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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

"dhnriverside" <da*@musoswire.com> wrote in message
news:06**********************************@microsof t.com...
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

Nov 19 '05 #2

P: n/a
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!! :o(
Dan
"Eliyahu Goldin" wrote:
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

"dhnriverside" <da*@musoswire.com> wrote in message
news:06**********************************@microsof t.com...
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


Nov 19 '05 #3

P: n/a
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

"dhnriverside" <da*@musoswire.com> wrote in message
news:14**********************************@microsof t.com...
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!! :o(
Dan
"Eliyahu Goldin" wrote:
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

"dhnriverside" <da*@musoswire.com> wrote in message
news:06**********************************@microsof t.com...
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


Nov 19 '05 #4

P: n/a
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
"Eliyahu Goldin" wrote:
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

"dhnriverside" <da*@musoswire.com> wrote in message
news:14**********************************@microsof t.com...
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!! :o(
Dan
"Eliyahu Goldin" wrote:
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

"dhnriverside" <da*@musoswire.com> wrote in message
news:06**********************************@microsof t.com...
> 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


Nov 19 '05 #5

P: n/a
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?
"dhnriverside" wrote:
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
"Eliyahu Goldin" wrote:
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

"dhnriverside" <da*@musoswire.com> wrote in message
news:14**********************************@microsof t.com...
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!! :o(
Dan
"Eliyahu Goldin" wrote:

> 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
>
> "dhnriverside" <da*@musoswire.com> wrote in message
> news:06**********************************@microsof t.com...
> > 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
>
>
>


Nov 19 '05 #6

P: n/a
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

"dhnriverside" <da*@musoswire.com> wrote in message
news:82**********************************@microsof t.com...
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?
"dhnriverside" wrote:
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
"Eliyahu Goldin" wrote:
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

"dhnriverside" <da*@musoswire.com> wrote in message
news:14**********************************@microsof t.com...
> 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!! :o(
>
>
> Dan
>
>
> "Eliyahu Goldin" wrote:
>
> > 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
> >
> > "dhnriverside" <da*@musoswire.com> wrote in message
> > news:06**********************************@microsof t.com...
> > > 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
> >
> >
> >

Nov 19 '05 #7

P: n/a
Hold on...

How does selecting an item in one list make the list go "ah yes, but I'm
also in this list as well, so update it list too". ?!!

The ListItem has no idea what list (or lists) it belongs to, so how can it
select itself in other lists?!
"Eliyahu Goldin" wrote:
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

"dhnriverside" <da*@musoswire.com> wrote in message
news:82**********************************@microsof t.com...
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?
"dhnriverside" wrote:
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
"Eliyahu Goldin" wrote:

> 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
>
> "dhnriverside" <da*@musoswire.com> wrote in message
> news:14**********************************@microsof t.com...
> > 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!! :o(
> >
> >
> > Dan
> >
> >
> > "Eliyahu Goldin" wrote:
> >
> > > 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
> > >
> > > "dhnriverside" <da*@musoswire.com> wrote in message
> > > news:06**********************************@microsof t.com...
> > > > 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
> > >
> > >
> > >
>
>
>


Nov 19 '05 #8

P: n/a
because "Selected" is a property of ListItem, not of ddl. By setting
..Selected, you don't select the item in the owner list, as in the case.of
setting .SelectedItem. Rather you set the item on it's own, and the owners
have to fight whether the item should be selected or no.

Eliyahu

"dhnriverside" <da*@musoswire.com> wrote in message
news:CE**********************************@microsof t.com...
Hold on...

How does selecting an item in one list make the list go "ah yes, but I'm
also in this list as well, so update it list too". ?!!

The ListItem has no idea what list (or lists) it belongs to, so how can it
select itself in other lists?!
"Eliyahu Goldin" wrote:
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

"dhnriverside" <da*@musoswire.com> wrote in message
news:82**********************************@microsof t.com...
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?
"dhnriverside" wrote:

> 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
>
>
> "Eliyahu Goldin" wrote:
>
> > 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
> >
> > "dhnriverside" <da*@musoswire.com> wrote in message
> > news:14**********************************@microsof t.com...
> > > 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!! :o(
> > >
> > >
> > > Dan
> > >
> > >
> > > "Eliyahu Goldin" wrote:
> > >
> > > > 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
> > > >
> > > > "dhnriverside" <da*@musoswire.com> wrote in message
> > > > news:06**********************************@microsof t.com...
> > > > > 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
> > > >
> > > >
> > > >
> >
> >
> >


Nov 19 '05 #9

P: n/a
But Im not using Item.Selected - i changed it to ddl.SelectedIndex= on your
recommendation. That's why I don't understand.........

"Eliyahu Goldin" wrote:
because "Selected" is a property of ListItem, not of ddl. By setting
..Selected, you don't select the item in the owner list, as in the case.of
setting .SelectedItem. Rather you set the item on it's own, and the owners
have to fight whether the item should be selected or no.

Eliyahu

"dhnriverside" <da*@musoswire.com> wrote in message
news:CE**********************************@microsof t.com...
Hold on...

How does selecting an item in one list make the list go "ah yes, but I'm
also in this list as well, so update it list too". ?!!

The ListItem has no idea what list (or lists) it belongs to, so how can it
select itself in other lists?!
"Eliyahu Goldin" wrote:
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

"dhnriverside" <da*@musoswire.com> wrote in message
news:82**********************************@microsof t.com...
> 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?
>
>
> "dhnriverside" wrote:
>
> > 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
> >
> >
> > "Eliyahu Goldin" wrote:
> >
> > > 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
> > >
> > > "dhnriverside" <da*@musoswire.com> wrote in message
> > > news:14**********************************@microsof t.com...
> > > > 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!! :o(
> > > >
> > > >
> > > > Dan
> > > >
> > > >
> > > > "Eliyahu Goldin" wrote:
> > > >
> > > > > 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
> > > > >
> > > > > "dhnriverside" <da*@musoswire.com> wrote in message
> > > > > news:06**********************************@microsof t.com...
> > > > > > 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
> > > > >
> > > > >
> > > > >
> > >
> > >
> > >


Nov 19 '05 #10

P: n/a
It matters only for the ddl you are setting for. The second one just finds
that one of its item suddenly became selected without any calls to
SelectedIndex.

Eliyahu

"dhnriverside" <da*@musoswire.com> wrote in message
news:89**********************************@microsof t.com...
But Im not using Item.Selected - i changed it to ddl.SelectedIndex= on your recommendation. That's why I don't understand.........

"Eliyahu Goldin" wrote:
because "Selected" is a property of ListItem, not of ddl. By setting
..Selected, you don't select the item in the owner list, as in the case.of setting .SelectedItem. Rather you set the item on it's own, and the owners have to fight whether the item should be selected or no.

Eliyahu

"dhnriverside" <da*@musoswire.com> wrote in message
news:CE**********************************@microsof t.com...
Hold on...

How does selecting an item in one list make the list go "ah yes, but I'm also in this list as well, so update it list too". ?!!

The ListItem has no idea what list (or lists) it belongs to, so how can it select itself in other lists?!
"Eliyahu Goldin" wrote:

> 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
>
> "dhnriverside" <da*@musoswire.com> wrote in message
> news:82**********************************@microsof t.com...
> > 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?
> >
> >
> > "dhnriverside" wrote:
> >
> > > 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
> > >
> > >
> > > "Eliyahu Goldin" wrote:
> > >
> > > > 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
> > > >
> > > > "dhnriverside" <da*@musoswire.com> wrote in message
> > > > news:14**********************************@microsof t.com...
> > > > > 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!! :o(
> > > > >
> > > > >
> > > > > Dan
> > > > >
> > > > >
> > > > > "Eliyahu Goldin" wrote:
> > > > >
> > > > > > 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
> > > > > >
> > > > > > "dhnriverside" <da*@musoswire.com> wrote in message
> > > > > > news:06**********************************@microsof t.com...
> > > > > > > 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
> > > > > >
> > > > > >
> > > > > >
> > > >
> > > >
> > > >
>
>
>


Nov 19 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.