asp實用類庫–DropDownList

來源:互聯網
上載者:User
<%
'<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
%>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.