<%
'<class>
'<name>DropDownList</name>
'<description><![CDATA[下拉選項框;作者:聖誕菠蘿包]]></description>
'<attributes>
' <attribute name="ID" comment="唯寫;下拉選項框ID"/>
' <attribute name="DataTextField" comment="顯示文本對應資料庫欄位的列號,如:{0};預設為{0};"/>
' <attribute name="DataValueField " comment="值對應資料庫欄位的列號;預設為{1};"/>
' <attribute name="SelectedIndex" comment="預設選項的索引"/>
' <attribute name="SelectedValue" comment="預設選項的值"/>
' <attribute name="Style" comment="下拉選項框樣式"/>
' <attribute name="IsList" comment="是否以列表形式展示"/>
' <attribute name="Multiple" comment="是否可多選;只有IsList=True才有效;"/>
' <attribute name="DataSource" comment="資料來源(斷開的記錄集)"/>
'</attributes>
'<methods>
' <method name="AddHeader(ByVal strText,ByVal strValue)" comment="添加選項頭"/>
' <method name="DataBind()" comment="資料繫結,顯示下拉選項框;"/>
'</methods>
'</class>
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
%>