DropDownList.FindByValue.Selected不工作......帮忙! [英] DropDownList.FindByValue.Selected not working... help!

查看:71
本文介绍了DropDownList.FindByValue.Selected不工作......帮忙!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好


这个下拉列表仍有问题。


基本上,我有4个。前2个工作很好,然后我的代码崩溃了

3rd。


ddlEndTimeHour.Items.FindByValue(endTime [0])。Selected = true;


其中endTime [0]是一个包含15的字符串。


我收到错误DropDownList不能选择多个项目。我知道

我没有在其他任何地方选择任何项目,虽然当我查看

生成的HTML时,第一项被标记为选中=已选择。我试过了

添加


ddlEndTimeHour.Items [0] .Selected = false;


但是没有帮助。


发生了什么事?!


干杯

Dan

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

推荐答案

Dan,


ddl始终至少选择一项,除非它是空的。你应该

设置SelectedItem或SelectedIndex属性来改变选择。


Eliyahu


" dhnriverside" < da*@musoswire.com>在留言中写道

news:06 ********************************** @ microsof t.com ...
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...
大家好

这个下拉列表仍有问题。

基本上,我有4个。前2个工作正常,然后我的代码在
3上崩溃。

ddlEndTimeHour.Items.FindByValue(endTime [0])。Selected = true;

endTime [0]是一个包含15的字符串。

我收到错误DropDownList不能选择多个项目。我知道我不会在其他任何地方选择任何项目,但是当我查看
生成的HTML时,第一项被标记为选中=已选择。我已经尝试过添加

ddlEndTimeHour.Items [0] .Selected = false;

但是没有帮助。

怎么回事?!

干杯

Dan
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



嗨Eliyahu,


感谢您的回复。不幸的是,这甚至更奇怪。


我理解DDL的理论,我认为Items.FindByValue

给你索引号,所以正在做.Selected = true与

相同..SelectedIndex = x;


然而,我已经尝试了它并得到了一些非常奇怪的行为......这是我的

上一篇关于这个主题的帖子,昨天发布.....


-------

好​​的,我在尝试根据数据库中存储的'b
选择DDL项时遇到一个奇怪的错误。


我的aspx页面上有4个DDL ...


< asp:dropdownlist id =" ddlStartTimeHour" RUNAT = QUOT;服务器" />

< asp:dropdownlist id =" ddlEndTimeHour" RUNAT = QUOT;服务器" />

< asp:dropdownlist id =" ddlStartTimeMinute" RUNAT = QUOT;服务器" />

< asp:dropdownlist id =" ddlEndTimeMinute" RUNAT = QUOT;服务器" />


这些在C#中声明为


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;


它们在Page_load上填充了几个for循环(09,10,11等

小时,00,15 ,分钟30,45)。然后,我试图根据DataReader选择正确的数据

如下..


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;


前两个(开始时间)似乎工作正常,但我在

EndTimeHour上出现错误; DropDownList不能选择多个项目。我不知道为什么会发生这种情况,即使我添加

ddlEndTimeHour.SelectedItem = -1也会发生这种情况。


甚至比这更奇怪...测试日期的StartTime是14:00,

结束时间是15:30。显然,theStartTIme可以工作,但是如果我注释掉2个

EndTime.Select行,那么EndTime DDL也会显示14:00!


它是'好像ddlStartTimeHour和ddlEndTimeHour是连接的,因此我得到

关于没有倍数的错误。但是*不能*是这样的......可以吗?!

------------


试过你的使用SelectedIndex的想法,发生了什么事情

是两个ddlStartTimeHour和ddlEndTimeHour在SelectedIndex语句中取得了给定

的值...


ddlEndTimeHour.SelectedIndex = 3;


我不明白为什么这两个DropDownLists似乎连接了?!!?


请帮忙!这真让我抓狂!! :o(

Dan

" Eliyahu Goldin"写道:
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总是至少选择一个项目,除非它是空的。你应该设置SelectedItem或SelectedIndex属性来改变选择。

Eliyahu

" dhnriverside"< ; da*@musoswire.com>在留言中写道
新闻:06 ******************************** ** @ microsof t.com ...
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...
大家好

这个下拉列表仍有问题。

基本上,我已经得到4.前2个工作正常,然后我的代码在
3上崩溃。

ddlEndTimeHour.Items.FindByValue(endTime [0])。Selected = true;

其中endTime [0]是一个包含15的字符串。

我收到错误DropDownList不能选择多个项目。我
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


知道

我在其他任何地方都没有选择任何物品,尽管我看着它他生成HTML,第一项标记为选中=选中。我已经尝试过添加

ddlEndTimeHour.Items [0] .Selected = false;

但是没有帮助。

怎么回事?!

干杯

Dan
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




Dan,


我不确定.SelectedIndex和.Selected是一样的。 .Selected是一个只有一个项目的

属性。它无法知道

其他项目的进展情况。然而.SelectedIndex是ddl的属性,而ddl可以首先取消选择之前选择的项目。


就你的奇怪问题而言,它是确实非常奇怪。仔细检查

aspx,ddls有什么方法可以引用相同的对象吗?


Eliyahu


dhnriverside < da*@musoswire.com>在消息中写道

news:14 ********************************** @ microsof t.com ...
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...
嗨Eliyahu,

感谢您的回复。不幸的是,这甚至更奇怪。

我理解DDL的理论,我假设Items.FindByValue
给你索引号,所以做.Selected = true是一样的因为
.SelectedIndex = x;

然而,我已经尝试了并且得到了一些非常奇怪的行为......这是我的
上一篇关于这个主题的帖子,昨天发布了......

-------
好的,我在尝试根据什么来选择DDL项目时遇到了一个奇怪的错误
存储在数据库中。

我的aspx页面上有4个DDL ...

< asp:dropdownlist id =" ddlStartTimeHour" RUNAT = QUOT;服务器" />
< asp:dropdownlist id =" ddlEndTimeHour" RUNAT = QUOT;服务器" />
< asp:dropdownlist id =" ddlStartTimeMinute" RUNAT = QUOT;服务器" />
< asp:dropdownlist id =" ddlEndTimeMinute" RUNAT = QUOT;服务器" />

这些在C#中声明为受保护的System.Web.UI.WebControls.DropDownList ddlStartTimeHour;
受保护的System.Web.UI.WebControls .DropDownList ddlStartTimeMinute;
受保护的System.Web.UI.WebControls.DropDownList ddlEndTimeHour;
受保护的System.Web.UI.WebControls.DropDownList ddlEndTimeMinute;

它们填充在Page_load上一对for循环(09,10,11等小时,00,15,30,45分钟)。然后,我试图根据DataReader选择正确的
数据,如下所示..

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;

前两个(开始时间)似乎工作正常,但是我收到错误在
上,EndTimeHour表示A DropDownList不能选择多个项目
。我不明白为什么会发生这种情况,即使我添加
ddlEndTimeHour.SelectedItem = -1也会发生。

甚至比那更奇怪...测试日期的StartTime是14:00 ,结束时间是15:30。显然,theStartTIme可以工作,但是如果我注释掉
2的EndTime.Select行,那么EndTime DDL也会显示14:00!

就像ddlStartTimeHour和ddlEndTimeHour一样,因此我得到了关于没有倍数的错误。但那个*不能*是这样的......可以吗?!
------------

尝试过使用SelectedIndex的想法,有什么意义呢?有
发生的是两个ddlStartTimeHour和ddlEndTimeHour取得了SelectedIndex语句中给出的值
...

ddlEndTimeHour.SelectedIndex = 3;

我不明白为什么这两个DropDownLists出现连接?!!?

请帮忙!这真让我抓狂!! :o(

Dan

" Eliyahu Goldin"写道:
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总是至少选择一个项目,除非它是空的。你
应该设置SelectedItem或SelectedIndex属性来改变选择。

Eliyahu

  dhnriverside"< ; da*@musoswire.com>在留言中写道
新闻:06 ******************************** ** @ microsof t.com ...
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...
大家好

这个下拉列表仍有问题。

基本上,我已经得到4.前2个工作正常,然后我的代码在第3个
崩溃。

ddlEndTimeHour.Items.FindByValue(endTime [0])。Selected = true;

其中endTime [0]是一个包含15的字符串。

我收到错误DropDownList不能选择多个项目。
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
知道
我不是在选择一个其他任何地方的项目,虽然当我查看
生成的HTML时,第一项被标记为选中=已选择。我已
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



尝试添加

ddlEndTimeHour.Items [0] .Selected = false;
<但这没有帮助。

发生了什么事?!

干杯

Dan


tried adding

ddlEndTimeHour.Items[0].Selected = false;

but that hasnt helped.

What''s going on?!

Cheers
Dan




这篇关于DropDownList.FindByValue.Selected不工作......帮忙!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆