Class DropDownList
Private ID_ '下拉选项框ID
Private dataTextField_ '显示文本对应数据库字段的列号,如:{0};默认为{0};
Private dataValueField_ '值对应数据库字段的列号;默认为{1};
Private selectedIndex_ '默认选项的索引
Private selectedValue_ '默认选项的值
Private style_ '样式
Private header_ '选项头信息
Private isList_ '是否为列表展示
Private multiple_ '是否可以多选
Private dataSource_ '数据源
Private recordCount_ '记录数
'返回下拉选项框ID
Public Property Get ID()
ID=ID_
End Property
'设置下拉选项框ID
Public Property Let ID(ByVal strID)
ID_=strID
End Property
'设置显示文本对应数据库字段的列号
Public Property Let DataTextField(ByVal strFieldNumber)
dataTextField_=strFieldNumber
End Property
'设置值对应数据库字段的列号
Public Property Let DataValueField(ByVal strFieldNumber)
dataValueField_=strFieldNumber
End Property
'设置默认选项的索引
Public Property Let SelectedIndex(ByVal intIndex)
If intIndex="" Or Not IsNumeric(intIndex) Or intIndex<0 Then
intIndex=-1
End If
selectedIndex_=intIndex
End Property
'设置默认选项的值
Public Property Let SelectedValue(ByVal Value)
selectedValue_=Value
End Property
'设置样式
Public Property Let Style(ByVal strStyle)
style_=strStyle
End Property
'设置是否为列表展示
Public Property Let IsList(ByVal boolValue)
If boolValue=True Then
isList_ =True
Else
isList_ =False
End If
End Property
'设置是否多选
Public Property Let Multiple(ByVal boolValue)
If boolValue=True Then
multiple_=True
Else
multiple_=False
End If
End Property
'设置数据源
Public Property Let DataSource(ByRef objRS)
Set dataSource_=objRS
recordCount_=dataSource_.RecordCount
End Property
'添加选项头
Public Function AddHeader(ByVal strText,ByVal strValue)
header_="<option value=""" & strValue & """>" & strText & "</option>" & VBCrlf
End Function
'绑定
Public Function DataBind()
Response.Write("<!--下拉选项框" & ID_ & "开始-->" & VBCrlf)
'判断是否列表展示
If isList_=True Then
If multiple_=True Then
Response.Write("<select name=""" & ID_ & """ id=""" & ID_ &""" class=""" & style_ &""" size=""" & recordCount_ &""" multiple=""multiple"">" & VBCrlf)
Else
Response.Write("<select name=""" & ID_ & """ id=""" & ID_ &""" class=""" & style_ &""" size=""" & recordCount_ &""">" & VBCrlf)
End If
Else
Response.Write("<select name=""" & ID_ & """ id=""" & ID_ &""" class=""" & style_ &""">" & VBCrlf)
End If
'输出选项头
Response.Write(header_)
'输出数据
dataSource_.MoveFirst()
Dim intRow
For intRow=0 To recordCount_-1
If (selectedIndex_<>-1 And selectedIndex_=intRow) Or (SelectedValue_<>"" And StrComp(SelectedValue_,ParseValueTemplate(dataValueField_),0)=0)Then
Response.Write("<option value=""" & ParseValueTemplate(dataValueField_) & """ selected=""selected"">" & ParseTextTemplate(dataTextField_) & "</option>" & VBCrlf)
Else
Response.Write("<option value=""" & ParseValueTemplate(dataValueField_) & """>" & ParseTextTemplate(dataTextField_) & "</option>" & VBCrlf)
End If
dataSource_.MoveNext()
Next
Response.Write("</select>" & VBCrlf)
Response.Write("<!--下拉选项框" & ID_ & "结束-->" & VBCrlf)
dataSource_.Close()
Set dataSource_=Nothing
End Function
'解析模板
Private Function ParseTemplate(ByVal strItemTemplate,ByRef arrTemplate,ByRef arrIndex)
If IsArray(arrTemplate) Then
For i=0 To UBound(arrTemplate)
If i<UBound(arrTemplate) Then
ParseTemplate=ParseTemplate & arrTemplate(i) & Eval(arrIndex(i))
Else
ParseTemplate=ParseTemplate & arrTemplate(i)
End If
Next
Exit Function
End If
Dim objRegExp,objMatches,strTemp,arrTemp,i,arrTempLen
Set objRegExp=new RegExp
objRegExp.Pattern = "({)(\d+)(})"
objRegExp.IgnoreCase = True
objRegExp.Global = True
strTemp=objRegExp.Replace(strItemTemplate,"{CHCW_SEPARATOR}")
arrTemp=Split(strTemp,"{CHCW_SEPARATOR}")
Set objMatches=objRegExp.Execute(strItemTemplate)
arrTempLen=UBound(arrTemp)
ReDim arrTemplate(arrTempLen)
ReDim arrIndex(arrTempLen-1)
For i=0 To arrTempLen
If i<arrTempLen Then
ParseTemplate =ParseTemplate & arrTemp(i) & dataSource_(Cint(objMatches(i).SubMatches(1)))
arrIndex(i)="dataSource_(" & objMatches(i).SubMatches(1) & ")"
Else
ParseTemplate = ParseTemplate & arrTemp(i)
End If
arrTemplate(i) = arrTemp(i)
Next
Set objMatches=Nothing
Set objRegExp=Nothing
End Function
Private arrValueTemplate,arrValueIndex '模板缓存数组,减少解析时
'解析模板
Private Function ParseValueTemplate(ByVal strItemTemplate)
ParseValueTemplate=ParseTemplate(strItemTemplate,arrValueTemplate,arrValueIndex)
End Function
Private arrTextTemplate,arrTextIndex '模板缓存数组,减少解析时
'解析模板
Private Function ParseTextTemplate(ByVal strItemTemplate)
ParseTextTemplate=ParseTemplate(strItemTemplate,arrTextTemplate,arrTextIndex)
End Function
'初始化
Private Sub Class_Initialize()
ID_="DropDownList1"
dataValueField_="{0}"
dataTextField_="{1}"
selectedIndex_=-1
selectedValue_=""
isList_ =False
multiple_=False
End Sub
'销毁
Private Sub Class_Terminate()
If Not dataSource_ Is Nothing Then
dataSource_.Close()
Set dataSource_=Nothing
End If
End Sub
End Class
%>
还有很多不足地方,请大家指正!
例子:
<!--#Include Virtual="/AspLib/Util/Configuration.asp"-->
<!--#Include Virtual="/AspLib/Util/DataAccess.asp"-->
<!--#Include Virtual="/AspLib/Control/DropDownList.asp"-->
<%
Set objDDL=new DropDownList
objDDL.ID="DropDownList1"
objDDL.AddHeader "请选择新闻分类",""
objDDl.DataSource=new DataAccess.ExecuteReader("Select NewsCategoryID,NewsCategoryName From News_Category")
objDDL.DataBind()
Set objDDL=Nothing
%>