错误无法在DropDownList中选择多个项目 [英] Error Cannot have multiple items selected in a DropDownList

查看:76
本文介绍了错误无法在DropDownList中选择多个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,我收到错误..无法在DropDownList中选择多个项目。当页面加载时,在DDLModality中选择ALL,但是当我将DDLModality.selectedvalue更改为任何值时,没有错误,但当我更改为ALL获取错误时再次出现错误。



This is my code and i am getting an error ..Cannot have multiple items selected in a DropDownList. when the page loads it select "ALL" in DDLModality but when i change DDLModality.selectedvalue to any "value" then no error but again when i change to "ALL "getting the error.

' USED TO ADD REFERRING PHYSICIAN IN THE DROPDOWN DYNAMICALLY 
    If CInt(Session("CenterID")) = 0 Then
        sql = "Select Ref_Phy_ID,Name from Ref_Phy_Master WHERE Ref_Phy_ID in(Select distinct Ref_Phy_ID from Patient_Details where Ref_Phy_ID <> '')"
    Else
        sql = "Select Ref_Phy_ID,Name from Ref_Phy_Master WHERE Ref_Phy_ID in(Select distinct Ref_Phy_ID from Patient_Details where Ref_Phy_ID <> '') And Center_ID = " & Session("CenterID")
    End If
    objDS = objFun.RunQuery(sql)
    ' USED TO REFRESH THE PAGE WHIN IT IS POSTED BACK 
    ' USED TO DISPLAY DEFAULT FIRST ITEM IN THE DROPDOWN 
    Dim Li1 As New ListItem()
    Li1.Text = "ALL"
    Li1.Value = ""
    cboRefPhy.Items.Add(Li1)
    ' USED TO COUNT THE STUDIES IN THE DROPDOWN 
    If (objDS.Tables(0).Rows.Count <> 0) Then
        ' USED TO CIRCULATE LOOP UPTO THE RECORD COUNT
        Dim i As Integer
        For i = 0 To objDS.Tables(0).Rows.Count - 1
            ' USED TO CREATE NEW ITEM IN THE DROPDOWN 
            Dim Li As New ListItem
            Li.Text = objDS.Tables(0).Rows(i)("Name").ToString()
            Li.Value = objDS.Tables(0).Rows(i)("Ref_Phy_ID").ToString()
            'USED TO ADD ITEMS IN THE DROPDOWN 
            cboRefPhy.Items.Add(Li)
        Next
    End If
    'USED TO SAVE THE CHANGES IN DATASET  
    objDS.AcceptChanges()
    ' USED TO CLOSE THE DATABASE CONNECTION 
    objDS.Dispose()
    cboRefPhy.ClearSelection()
    cboRefPhy.SelectedValue = Convert.ToString(Request.Form("cboRefPhy"))

    'USED TO ADD MODALITY IN THE DROPDOWN DYNAMICALLY
    If CInt(Session("CenterID")) = 0 Then
        sqlStudy = "Select Modality_ID,Modality from Hospital_Modality_Master WHERE Modality_ID in(Select distinct Study_ID from Patient_Details where Study_ID <> '')"
    Else
        sqlStudy = "Select Modality_ID,Modality from Hospital_Modality_Master WHERE Modality_ID in(Select distinct Study_ID from Patient_Details where Study_ID <> '') And Center_ID = " & Session("CenterID")
    End If
    'Dim objDS As New DataSet()
    objDS = objFun.RunQuery(sqlStudy)
    ' USED TO REFRESH THE PAGE WHIN IT IS POSTED BACK 
    ' USED TO DISPLAY DEFAULT FIRST ITEM IN THE DROPDOWN 
    'Dim Li1 As New ListItem()
    Li1.Text = "ALL"
    Li1.Value = ""
    ' Dim all As String
    ' all = "All"
    'Ddl_Modality.Items.Add(all)
    DDLModality.Items.Add(Li1)
    ' USED TO COUNT THE STUDIES IN THE DROPDOWN 
    If (objDS.Tables(0).Rows.Count <> 0) Then
        ' USED TO CIRCULATE LOOP UPTO THE RECORD COUNT
        Dim i As Integer
        For i = 0 To objDS.Tables(0).Rows.Count - 1
            ' USED TO CREATE NEW ITEM IN THE DROPDOWN 
            Dim Li As New ListItem
            Li.Text = objDS.Tables(0).Rows(i)("Modality").ToString()
            Li.Value = objDS.Tables(0).Rows(i)("Modality_ID").ToString()
            'USED TO ADD ITEMS IN THE DROPDOWN 
            DDLModality.Items.Add(Li)
        Next
    End If
    'USED TO SAVE THE CHANGES IN DATASET  
    objDS.AcceptChanges()
    ' USED TO CLOSE THE DATABASE CONNECTION 
    objDS.Dispose()
    DDLModality.ClearSelection()
    DDLModality.SelectedValue = Convert.ToString(Request.Form("DDLModality"))


    ' USED TO ADD STUDY IN THE DROPDOWN DYNAMICALLY 
    If CInt(Session("CenterID")) = 0 Then
        sqlStudy = "Select Study_ID,Study_Desc from Study_Master WHERE Study_ID in(Select distinct Study_ID from Patient_Details where Study_ID <> '')"
    Else
        sqlStudy = "Select Study_ID,Study_Desc from Study_Master WHERE Study_ID in(Select distinct Study_ID from Patient_Details where Study_ID <> '') And Center_ID = " & Session("CenterID")
    End If

    If (DDLModality.SelectedItem.Text <> "ALL") Then
        sqlStudy = sqlStudy & " AND Modality = '" & DDLModality.SelectedItem.Text & "'"

    End If

推荐答案

不能在DropDownList中选择多个项目 [ ^ ]


Dim Li2 As New ListItem()

Li2.Text =ALL

Li2.Value =

DDLModality.Items.Add(Li2 )
Dim Li2 As New ListItem()
Li2.Text = "ALL"
Li2.Value = ""
DDLModality.Items.Add(Li2)


首先在dropDown中选择任何值之前,我们应该首先检查它是否存在(只是为了确保并避免意外情况)



The very first thing is before selecting any value in dropDown we should first check if it is present or not (just to make sure and avoid the unexpected scenarios)

if(ddlDropDown.Items.FindByValue("ValueToSearch")!=null)
{
     ddlDropDown.Items.FindByValue.Selected=true;
}







现在让我们来看看你的错误不能选择多个项目DropDownList

当我们尝试通过下拉菜单中可能有两个项目的选择标准时,通常会出现此错误。



让我们说如果您的下拉列表中有两个值为的项目,并且您已编写以下代码




Now lets come to your error "Cannot have multiple items selected in a DropDownList"
This error generally comes when we try to pass selection criteria for which there may be two items in the dropdown.

Let's say if your dropdown have two items with the value "" and you have written following code

DDLDropDown.SelectedValue = "";





然后会抛出此异常。



最简单的方法是从浏览器检查您的页面来源,看看您的下拉列表中有哪些最终项目。



当添加新的listItem时还有一件事,如果你想使用相同的变量,那么至少要重新初始化它





then it will throw this exception.

the simplest way is to check your page source from browser and see what final items were there in your dropdown.

Also one more thing when adding a new listItem, if you want to use same variable then at least reinitialize it

Li1 = new ListItem();
Li1.Text = "ALL";
Li1.Value = ""
DDLModality.Items.Add(Li1)









希望这可以帮到你。





Hope this helps you.


这篇关于错误无法在DropDownList中选择多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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