从下拉列表ASP中排除某些选择

时间:2015-12-15 12:05:25

标签: asp-classic html-select dropdown

我正在使用ASP(在支付网关内)。从数据库中拉入的下拉选项。我无法触及数据库,因此必须从代码中解决问题。

以下是我需要排除_01BM(Q)

的值名称

以下是下拉列表的代码。

<select name="programgroup" onchange="onProgramGroup()">                    
<% Call buildDropDownList(strProgramGroupCode, rsProgramGroup, "ProgramGroupCode", "ProgramGroupDescription", False)%>
</select>

我真的很感激这些人的任何帮助。

以下是该方法的代码:

Sub buildDropDownList(strCurrentSelection, objListData, strCodeName, strDescriptionName, blnIncludeOther)
    If Not objListData.BOF Then
        objListData.MoveFirst
    End If
    While Not objListData.EOF
        Response.Write "<option value='" & objListData(strCodeName) & "' "
        If StrComp(strCurrentSelection, objListData(strCodeName),1) = 0 then
            Response.Write "selected"
        End If
        Response.Write ">" & objListData(strDescriptionName) & "</option>" & VbCrLf
        objListData.MoveNext
    Wend

    if blnIncludeOther then
        Response.Write "<option value='<Other>' "
        If strCurrentSelection <> "" and InStr(1, "<Other>", strCurrentSelection) = 1 then
            Response.Write "selected"
        End If
        Response.Write ">Other</option>" & VbCrLf
    end if
End Sub

1 个答案:

答案 0 :(得分:1)

您必须更改构建下拉列表的方法。由于您没有为我们提供代码,因此我将为您提供一个框架,并可以使用它来更改您的实际代码。

为了使它更通用,更简单,最好将值作为数组传递给方法,而不是在那里对它们进行硬编码。

因此,该方法应如下所示:

Sub buildDropDownList(strProgramGroupCode, rsProgramGroup, someParamHere, anotherParam, boolParam, arrValuesToExclude)
    Dim excludedValuesMapping, x
    Set excludedValuesMapping = Server.CreateObject("Scripting.Dictionary")
    For x=0 To UBound(arrValuesToExclude)
        excludedValuesMapping.Add(LCase(arrValuesToExclude(x)), True)
    Next

    '...unknown code here...

    Do Until rsProgramGroup.EOF
        strCurrentValue = rsProgramGroup(valueFieldName)
        If excludedValuesMapping.Exists(LCase(strCurrentValue)) Then
            'value should be excluded, you can do something here, but not write it to browser
        Else  
            strCurrentText = rsProgramGroup(textFieldName)
            Response.Write("<option value=""" & Replace(strCurrentValue, """", "&quot;") & """>" & strCurrentText & "</option>")
        End If
        rsProgramGroup.MoveNext
    Loop
End Sub

使用它:

<% Call buildDropDownList(strProgramGroupCode, rsProgramGroup, "ProgramGroupCode", "ProgramGroupDescription", False, Array("_01BM(Q)"))%>

现在拥有您的代码,如果您不想使其成为通用代码,您也可以通过在buildDropDownList子代码中包含此类代码来忽略特定值:

Dim currentCodeValue
While Not objListData.EOF
    currentCodeValue = objListData(strCodeName)
    If (UCase(currentCodeValue)<>"_04GIDBM") And _
        (UCase(currentCodeValue)<>"_05GIDFM") And _ 
        (UCase(currentCodeValue)<>"_08EXHRM") And _ 
        (UCase(currentCodeValue)<>"_10EXMKT") And _ 
        (UCase(currentCodeValue)<>"_12EXTTH") And _ 
        (UCase(currentCodeValue)<>"_17EXHSC") Then
        Response.Write "<option value='" & currentCodeValue & "' "
        If StrComp(strCurrentSelection, currentCodeValue, 1) = 0 then
            Response.Write "selected"
        End If
        Response.Write ">" & objListData(strDescriptionName) & "</option>" & VbCrLf
    End If
    objListData.MoveNext
Wend

这将跳过具有此类值的任何记录,并且不会为它们输出下拉选项。