所选值无效 [英] Selected Value Invalid

查看:96
本文介绍了所选值无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的下拉菜单让我很讨厌,我一生无法弄清原因.当我尝试填充它时,它会给我:

< control>的SelectedValue无效,因为它不存在于项目列表中

一旦我尝试进行数据绑定,即使我检查了选定的值(它是1),它也会因该错误而失败,因此我检查了数据源(直接在即时窗口中使用Directcast),确保表中的第一行足够ID为1.所以我有点困惑.我没有理由看到它失败.还有其他人遇到过吗?

代码如下:

So I have a dropdown that is giving me crap and I can''t for the life of me figure out why. When I try to populate it it gives me:

<control> has a SelectedValue which is invalid because it does not exist in the list of items

As soon as I try to databind it fails with that error even though I checked the selected value (it''s 1) so then I checked the datasource (directly using directcast in the immediate window) sure enough the very first row in the table has an ID of 1. So I''m a little perplexed. There''s no reason I can see for it to fail. Anyone else ever come across this?

Here''s the code:

Private Sub PopulateProbers()
    With ddlProber
        .DataSource = CD.GetProberList()
        .DataTextField = "Name"
        .DataValueField = "ID"
        'Total hack. This control for no readily apparent reason fails to populate the second time. None
        'of the other ones have a problem, just this unique little snowflake. I verified that the selected item
        'actually IS in the table that I'm attempting to databind so your guess is as good as mine... I'm
        'skipping it if it fails.
        Try
            .DataBind()
        Catch
        End Try

        Dim LI As New ListItem
        LI.Text = "Unselected"
        LI.Value = "Unknown"
        .Items.Insert(0, LI)
        .Enabled = True
        Try
            .SelectedValue = .Attributes("LastValue")
        Catch ex As Exception
            .SelectedValue = "Unknown"
        End Try
    End With
End Sub



应该注意的是,这与第一次填充它的代码完全相同.还应注意,我还有其他15个类似的填充例程,它们可以正常工作.



It should be noted that this is the exact same piece of code that populated it the first time. It should also be noted that I have 15 other populate routines exactly like it that work fine. It''s just THIS one that is messing with my head!

推荐答案

在分配SelectedValue时,请确保在分配时将ID转换为字符串.整数和字符串是非常不同的东西.即1!="1".
When you assign SelectedValue, make sure that your ID is converted to string on assignment. An integer and a string are very different things. i.e. 1 != "1".


这篇关于所选值无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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