| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Create Dynamic Selectbox Sub Routine
I'm sure there is something out there that does this but I went ahead and tried to come up with something myself.
Instead of writing the same code over and over simply call a subroutine to create a selectbox. This is a very simple setup but is modular and may come in handy for those tired of creating selectboxes via manual means. Experiment with it. Make it better if you wish. The actual Sub Routine Code:
Sub CreateSelectBox(selectbox_name, selectbox_value, selectbox_output, _
table_name, vRequestedFormValue, bJavaScriptSubmit, bSelectOption)
'selectbox_name ' Name of HTML Selectbox
'selectbox_value ' Database Field Name that holds HTML Selectbox Option Value
'selectbox_output ' Database Field Name that holds HTML Selectbox Option Output
'table_name ' Database Table Name
'vRequestedFormValue ' Submitted Selectbox Form Value - variable type clng/string
'bJavaScriptSubmit ' Boolean Option to use Javascript Submit if Desired (true/false)
'aSelectboxArray ' Array to Store Selectbox Values/Output
'sSubmitOption ' Turns on Javascript Submit if bJavaScriptSubmit is True
'bSelectOption ' Boolean Option to Select a 'Postback' Form Submission
' Requires a Form Variable to Work
On Error Resume Next
Set oRs = oConn.Execute("SELECT [" & selectbox_value & "],[" & selectbox_output & "] " & _
"FROM " & table_name & ";")
If Err.number <> 0 then
Select Case Err.number
Case -2147217904
Response.Write "<p>Error Number: " & Err.number & "<br>" & _
"<p>It seems you may have entered a field that does not exist in " & table_name & "<br> " & _
"When calling the function, ensure field names are spelled correctly</p>"
Case Else
Response.Write "<p>Error Number: " & Err.number & "<br>" & _
"Description: " & Err.description & "</p>"
End Select
Response.End
End If
If Not oRs.EOF Then
aSelectboxArray = oRs.GetRows()
oRs.Close
Set oRs = Nothing
End If
If isNumeric(vRequestedFormValue) Then
vRequestedFormValue = CLNG(vRequestedFormValue)
End If
If bJavascriptSubmit Then
sSubmitOption = " onChange=""submit()"""
Else
sSubmitOption = ""
End If
Response.Write vbCrlf & _
"<select name=""" & selectbox_name & """" & sSubmitOption & ">" & vbCrlf & _
" <option value=""null"">" & vbCrlf
If isArray(aSelectboxArray) Then
For i = 0 to UBOUND(aSelectboxArray,2)
Response.Write " <option value=""" & aSelectboxArray(0,i) & """"
If vRequestedFormValue = aSelectboxArray(0,i) AND _
bSelectOption Then Response.Write " selected"
Response.Write " " & iSectionID & aSelectboxArray(1,i) & ">" & aSelectboxArray(1,i) & vbCrlf
Next
End If
Response.Write "</select>"
End Sub
Example of how to implement Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
<!-- #include file = "procedures/ConnectDatabase.asp" -->
</head>
<body>
<form action="<%=Request.ServerVariables("SCRIPT_NAME")%>" method="post">
<%
Dim oConn
Sub CreateSelectBox(selectbox_name, selectbox_value, selectbox_output, _
table_name, vRequestedFormValue, bJavaScriptSubmit, bSelectOption)
'selectbox_name ' Name of HTML Selectbox
'selectbox_value ' Database Field Name that holds HTML Selectbox Option Value
'selectbox_output ' Database Field Name that holds HTML Selectbox Option Output
'table_name ' Database Table Name
'vRequestedFormValue ' Submitted Selectbox Form Value - variable type clng/string
'bJavaScriptSubmit ' Boolean Option to use Javascript Submit if Desired (true/false)
'aSelectboxArray ' Array to Store Selectbox Values/Output
'sSubmitOption ' Turns on Javascript Submit if bJavaScriptSubmit is True
'bSelectOption ' Boolean Option to Select a 'Postback' Form Submission
' Requires a Form Variable to Work
On Error Resume Next
Set oRs = oConn.Execute("SELECT [" & selectbox_value & "],[" & selectbox_output & "] " & _
"FROM " & table_name & ";")
If Err.number <> 0 then
Select Case Err.number
Case -2147217904
Response.Write "<p>Error Number: " & Err.number & "<br>" & _
"<p>It seems you may have entered a field that does not exist in " & table_name & "<br> " & _
"When calling the function, ensure field names are spelled correctly</p>"
Case Else
Response.Write "<p>Error Number: " & Err.number & "<br>" & _
"Description: " & Err.description & "</p>"
End Select
Response.End
End If
If Not oRs.EOF Then
aSelectboxArray = oRs.GetRows()
oRs.Close
Set oRs = Nothing
End If
If isNumeric(vRequestedFormValue) Then
vRequestedFormValue = CLNG(vRequestedFormValue)
End If
If bJavascriptSubmit Then
sSubmitOption = " onChange=""submit()"""
Else
sSubmitOption = ""
End If
Response.Write vbCrlf & _
"<select name=""" & selectbox_name & """" & sSubmitOption & ">" & vbCrlf & _
" <option value=""null"">" & vbCrlf
If isArray(aSelectboxArray) Then
For i = 0 to UBOUND(aSelectboxArray,2)
Response.Write " <option value=""" & aSelectboxArray(0,i) & """"
If vRequestedFormValue = aSelectboxArray(0,i) AND _
bSelectOption Then Response.Write " selected"
Response.Write " " & iSectionID & aSelectboxArray(1,i) & ">" & aSelectboxArray(1,i) & vbCrlf
Next
End If
Response.Write "</select>"
End Sub
iUserID = Request.Form("user_id")
iDepartmentID = Request.Form("department_id")
ConnectDatabase "Customers"
' a selectbox that holds a list of users
' do not want a javascript submit
CreateSelectBox "user_id", "UserID", "LastName", "tblUsers", _
iUserID, false, true
' want another select box?...why not?
' as long from as from the same database
' and you have a connection... no problem!
' create a list of departments and use javascript to
' submit using onchange
CreateSelectBox "department_id", "DepartmentID", "Department", "tblDepartments", _
iDepartmentID, true, true
%>
</form>
</body>
</html>
|
|
#2
|
|||
|
|||
|
Looks good
![]() There is a problem with the HTML output of the option Code:
Response.Write " " & iSectionID & aSelectboxArray(1,i) & ">" & aSelectboxArray(1,i) & vbCrlf What is iSectionID? I don't see it defined anywhere and even if it was it is being output in the wrong place. The line should be Code:
Response.Write ">" & aSelectboxArray(1,i) & vbCrlf If you want to be XHTML compatible then you need to close tags and cannot use attribute minimization. The option tag requires a closing tag. Code:
</option> You would need to use Code:
selected="selected"
__________________
CyberTechHelp |
![]() |
| Viewing: ASP Free Forums > Programming > Code Bank > Create Dynamic Selectbox Sub Routine |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|