Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old November 28th, 2006, 03:18 PM
user101 user101 is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Posts: 409 user101 User rank is Sergeant (500 - 2000 Reputation Level)user101 User rank is Sergeant (500 - 2000 Reputation Level)user101 User rank is Sergeant (500 - 2000 Reputation Level)user101 User rank is Sergeant (500 - 2000 Reputation Level)user101 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 6 Days 12 h 45 sec
Reputation Power: 12
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>

Reply With Quote
  #2  
Old November 29th, 2006, 05:50 AM
degsy degsy is offline
Contributing User
ASP Free God 2nd Plane (6000 - 6499 posts)
 
Join Date: Aug 2005
Location: North East, UK
Posts: 6,191 degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 19 h 41 m 52 sec
Reputation Power: 121
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

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingCode Bank > Create Dynamic Selectbox Sub Routine


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway