2017-06-13 71 views
1

我尝试使用下面的代码C#SelectedIndex的错误

update_facility_shift_duration.SelectedIndex = Int32.Parse(reader_facility["facility_shift_duration_id"].ToString()); //If this is current user's facility add select 

update_facility_shift_duration对于<selected>其中由下面的代码获取其值的ID

update_facility_shift_duration.Items.Add(new ListItem(result, reader_facility["facility_shift_duration_id"].ToString())); 

我有大约6选择选项和想要选择与数据库中的ID相匹配的那个。

但我收到以下错误。有谁知道如何解决这个问题,或者可能有一些想法将我推向正确的方向? 在此先感谢

Error

+1

您是否有任何方法来验证facility_shift_duration_id的内容?它看起来像试图在reader_facility []中找到不存在的东西 – Floaterz

+0

您是否正在初始化您在'Page_Init'中的项目集合?如果您想在“Page_Load”中使用客户端提供的选定索引,这可能至关重要。 – grek40

+1

请在分配“SelectedIndex”之前检查'update_facility_shift_duration.Items.Count'。确保有足够的物品。 – grek40

回答

1

reader_facility [ “facility_shift_duration_id”]可能是零。检查它是空的还是空的。

+0

是啊如果我'Response.Write(reader_facility [“facility_shift_duration_id”]。ToString());'它填充一个'2' –

+0

您正在设置ListItem值作为facility_shift_duration_id。然后你试图从索引中找到这个项目,但是你给的价值是你的ID。尝试selectedValue而不是selectedIndex。 –

+0

如果我尝试这样做,我会得到以下消息:'htmlSelect'不包含定义'selectedValue',并且没有找到接受类型'HtmlSelect'的第一个参数的扩展方法'selectedValue'。 –