ASP practical class library-dropdownlist

Source: Internet
Author: User
<%
'<Class>
'<Name> dropdownlist </Name>
'<Description> <! [CDATA [drop-down box; Author: Christmas pineapple package]> </description>
'<Attributes>
'<Attribute name = "ID" comment = "write only; drop-down option box ID"/>
'<Attribute name = "datatextfield" comment = "display the column number of the database field corresponding to the text, for example, {0}; default value: {0};"/>
'<Attribute name = "datavaluefield" comment = "value corresponds to the column number of the database field; default value: {1};"/>
'<Attribute name = "selectedindex" comment = "Default Option Index"/>
'<Attribute name = "selectedvalue" comment = "default option value"/>
'<Attribute name = "style" comment = "drop-down option box style"/>
'<Attribute name = "islist" comment = "list display"/>
'<Attribute name = "multiple" comment = "whether multiple options are allowed; valid only when islist = true;"/>
'<Attribute name = "datasource" comment = "Data Source (disconnected record set)"/>
'</Attributes>
'<Methods>
'<Method name = "addheader (byval strtext, byval strvalue)" comment = "add option Header"/>
'<Method name = "databind ()" comment = "data binding. The drop-down box is displayed."/>
'</Methods>
'</Class>

Class dropdownlist

Private ID _ 'drop-down box ID
Private datatextfield _ 'displays the column number of the database field corresponding to the text, for example, {0}; default value: {0 };
The private datavaluefield _ 'value corresponds to the column number of the database field. The default value is {1 };
Private selectedindex _ 'default option Index
Private selectedvalue _ 'Default Value
Private style _ 'style
Private header _ 'option header information
Whether Private islist _ 'is displayed as a list
Private multiple _ 'can be selected multiple times?
Private datasource _ 'Data Source
Private recordcount _ 'number of records
 
'Return the drop-down box ID
Public property get ID ()
Id = ID _
End Property
 
'Set the drop-down option box ID
Public property let ID (byval Strid)
ID _ = Strid
End Property
 
'Set the column number of the database field corresponding to the displayed text
Public property let datatextfield (byval strfieldnumber)
Datatextfield _ = strfieldnumber
End Property
 
'Set the column number corresponding to the database Field
Public property let datavaluefield (byval strfieldnumber)
Datavaluefield _ = strfieldnumber
End Property
 
'Set the index of the default option
Public property let selectedindex (byval intindex)
If intindex = "" or not isnumeric (intindex) or intindex <0 then
Intindex =-1
End if
Selectedindex _ = intindex
End Property
 
'Set the default option value
Public property let selectedvalue (byval value)
Selectedvalue _ = Value
End Property
 
'Set the style
Public property let style (byval strstyle)
Style _ = strstyle
End Property
 
'Set whether to display the list
Public property let islist (byval boolvalue)
If boolvalue = true then
Islist _ = true
Else
Islist _ = false
End if
End Property
 
'Set whether to select multiple
Public property let multiple (byval boolvalue)
If boolvalue = true then
Multiple _ = true
Else
Multiple _ = false
End if
End Property
 
'Set the data source
Public property let datasource (byref objrs)
Set datasource _ = objrs
Recordcount _ = datasource _. recordcount
End Property
 
'Add option Headers
Public Function addheader (byval strtext, byval strvalue)
Header _ = "<option value =" & strvalue & ""> "& strtext &" </option> "& vbcrlf
End Function
 
'Bind
Public Function databind ()
Response. Write ("<! -- Drop-down option box "& ID _ &" start --> "& vbcrlf)
'Determine whether the list is displayed
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
'Output option Headers
Response. Write (header _)
'Output data
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 ("<! -- Drop-down option box "& ID _ &" end --> "& vbcrlf)
Datasource _. Close ()
Set datasource _ = nothing
End Function
 
'Parsing Template
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 and arrvalueindex templates cache arrays to reduce resolution time
 
'Parsing Template
Private function parsevaluetemplate (byval stritemtemplate)
Parsevaluetemplate = parsetemplate (stritemtemplate, arrvaluetemplate, arrvalueindex)
End Function
 
Private arrtexttemplate, arrtextindex 'template caches an array to reduce resolution time
 
'Parsing Template
Private function parsetexttemplate (byval stritemtemplate)
Parsetexttemplate = parsetemplate (stritemtemplate, arrtexttemplate, arrtextindex)
End Function

'Initialization
Private sub class_initialize ()
ID _ = "dropdownlist1"
Datavaluefield _ = "{0 }"
Datatextfield _ = "{1 }"
Selectedindex _ =-1
Selectedvalue _ = ""
Islist _ = false
Multiple _ = false
End sub

'Destroy
Private sub class_terminate ()
If not datasource _ is nothing then
Datasource _. Close ()
Set datasource _ = nothing
End if
End sub
 
End Class
%>

There are still many shortcomings. please correct me!

Example:

<! -- # 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 "select news Classification ",""
Objddl. datasource = new dataaccess. executereader ("select newscategoryid, newscategoryname from news_category ")
Objddl. databind ()
Set objddl = nothing
%>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.