Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Include file use for html

Status
Not open for further replies.

bobregion

IS-IT--Management
May 24, 2001
4
US
Hi,

I was wondering if there was a way to 'include' text used with the <select> tag in ASP. I found that if I included an "include file" reference at the location in the ASP page I want to insert the text I would get what I wanted. BUT, is it good programming or is there a more appropriate way to do what I need done.

I will over simplify my example and hopefully illicit some great threads!

The problem:

Several <select> statements use the same <option>s. I'd like to have those in an external file and reference them when I need them. It'd make the ASP page MUCH less cluttered.


The setup:

'option' file
option.asp
-------start-------------
<%%>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<%%>
--------end--------------
form.asp
-------start-------------
<html>
<body>
...<select size="1" name="option1">
......<!--#include file="option.asp"-->
...</select>
...<select size="1" name="option2">
......<!--#include file="option.asp"-->
...</select>
...<select size="1" name="option3">
......<!--#include file="option.asp"-->
...</select>
</body>

</html>
--------end--------------

So, again, I have simply taken a LONG list of options and using the include file feature shortened the creation of certain forms. My testing works so far, but is it the correct way to do this or is this a major kludge?

 
I believe that the performance of the page drops due to reading the include again and again and again ...

Alternative 1: write a loop eg:
Code:
<%
for i = 1 to 3
 Response.Write "<select size=""1"" name=""option" & i &_
 """>" &_
 "<option>1" &_
 "<option>2" &_
 "<option>3" &_
 "</select>"
next



Alternative 2: write a User Defined Function. eg:
Code:
function Opties
 Response.write "<option>1<option>2<option>3"
end function

%>

<select size="1" name="option1">
<% Opties() %>
</select>

<select size="1" name="option2">
<% Opties() %>
</select>

<select size="1" name="option2">
<% Opties() %>
</select>


(or combine these ideas. you wrote that you simplyfied the code, so i presume there is a lot more code between the <SELECTS>'s, which may make alternative 1 no option....


ttmug.gif
 
I looked at the dynamic generations of the <option> tags. I am working on code to do that.

I just recall from many years ago that there is a way to include text in a similar way...and I think the performance hit was the gotcha. But it was very small...

I'll keep looking!

Thank you for you input!
 
I once struggled with the question how to ommit FRAMES but still repeat a header and menu on every webpage, with only 1 file with the menu.
That resulted in including text this way:

Code:
<html>
<script language="JavaScript1.2" type="text/javascript" src="header.js"></script>
</HTML>


And header.js:
Code:
document.write ('<H1>Hello world!</H1>');


(Of course same performance side effects as INCLUDE)

Looking at performance: if you have *many* <SELECT>'s i recommend not to use
Code:
<select size="1" name="option2">
<% Opties() %>
</select>

because this switches between HTML output and code processing. This is faster:
Code:
<%
response.write "<select size=""1"" name=""option2"">"
Opties()
response.write "</select>"
%>

ttmug.gif
 
Oke, so after yesterday afternoon im into benchmarking

I changed the Tarwn/Foxbox Benchmark to answer my own "believe":

Code:
<%
'------------------------------------------------
' Tek-tips thread333-790823 benchmark
' Written 2004/03/08 by Foxbox, based on benchmark 
' written by Tarwn
'------------------------------------------------ 
' Method 1: Options in asp include file
' Method 2: Options in javascript include file
' Method 3: Options in UDF
' Method 4: Options in Array
' Method 5: loop with mix ASP/HTML
' Method 6: loop without mix
'------------------------------------------------
' Contents will be Response.Cleared afterwards so 
'    we don't have to deal with the browser output.
' This should not make a difference in the timer
'    outcome.
Dim m_time(6)
Dim m_avg(6), m_low(6), m_high(6)
Dim m_title(6)
Dim s_time

m_title(1) = "Options in option.asp"
m_title(2) = "Options in option.js"    
m_title(3) = "Options in UDF"
m_title(4) = "Options in array"
m_title(5) = "Loop with mix"
m_title(6) = "Loop without mix"


'------------------ Workhorse -------------------
' Runs te test code and displays results
Dim num_tests, num_runs
num_tests = 10000   ' number of <select>'s per page
num_runs = 10


Dim run_counter, method_counter

'initialize low, high, total time values
For method_counter = 1 to 6
    m_time(method_counter) = 0
    m_low(method_counter) = 100000
    m_high(method_counter) = 0
Next

'run the tests
Dim temp_time
For run_counter = 1 to num_runs
    For method_counter = 1 to 6
        temp_time = Runtest(method_counter, num_tests)    'get test results
        If temp_time = -1 Then
            Response.Write "Error Occurred: mc=" & method_counter & " rc=" & run_counter
            Response.End
        End If

        m_time(method_counter) = m_time(method_counter) + temp_time    'add time to total time for this method
        If temp_time < m_low(method_counter) Then m_low(method_counter) = temp_time    'if this is lowest, substitute
        If temp_time > m_high(method_counter) Then m_high(method_counter) = temp_time    'if this is highest, substitute
        If run_counter = num_runs Then m_avg(method_counter) = m_time(method_counter)/num_runs    'if last run then calc average

        temp_time = -1    'insurance on next loop
    Next
Next

Response.Write "<table style=""display:inline;""><tr><th colspan=""3"">Benchmark Results</th></tr>"
Response.Write "<tr><th>Method</th><th>Name</th><th>Average</th><th>Low</th><th>High</th></tr>"
For method_counter = 1 to 6
    Response.Write "<tr><td>Method #" & method_counter & "</td><td>" & m_title(method_counter) & "</td><td>" & FormatNumber(m_avg(method_counter),3) & "</td><td>" & FormatNumber(m_low(method_counter),3) & "</td><td>" & FormatNumber(m_high(method_counter),3) & "</td></tr>"
Next
Response.Write "</table><br>"
Response.Write "Times are averages from " & num_runs & " runs of " & num_tests & " loops for each method."


'------------------ Test Functions --------------
' These are the functions to handle the actual 5
' method tests
Function Method1(tCount)
    StartClock
    %>
    <h1>INCLUDE OPTION.ASP</h1>
    <%
    dim i
    for i = 1 to tCount
      response.Write "<select name=option" & i & ">"     
      %>
      <!-- #include file="option.asp" -->      
      </select>
      <%
      response.Write "</select>"     
    next
    Method1 = StopClock
    Response.Clear
End Function

Function Method2(tCount)
    StartClock
    %>
    <h1>INCLUDE OPTION.JS</h1>
    <%
    for i = 1 to tCount
      response.Write "<select name=option" & i & ">"     
      %>
      <script language="javascript" src="option.js"></script>      
      <%
      response.Write "</select>"     
    next

    Method2 = StopClock
    Response.Clear
End Function

function Opties
     response.Write "<option>1</option><option>2</option><option>3</option>"
    end function

Function Method3(tCount)
    StartClock
    %>
    <h1>OPTIONS IN UDF</h1>
    <%
    for i = 1 to tCount
      response.Write "<select name=option" & i & ">"     
      Opties()
      response.Write "</select>"     
    next
    Method3 = StopClock
    Response.Clear
End Function

Function Method4(tCount)
    StartClock
    %>
    <h1>OPTIES IN ARRAY</h1>
    <%
    dim optie(2)
    optie(0) = "1"
    optie(1) = "2"
    optie(2) = "3"

    for i = 1 to tCount
      response.Write "<select name=option" & i & ">"     
      for y = 0 to 2
       response.Write "<option>" & Optie(y) & "</option>"
      next
      response.Write "</select>"     
    next

    Method4 = StopClock
    Response.Clear
End Function


Function Method5(tCount)
    StartClock
    %>
    <h1>LOOP WITH MIX</h1>
    <%
    for i = 1 to tCount
      %>
      <select name=option<%= i %>> 
       <option>1</option>
       <option>2</option>
       <option>3</option>
       </select>
    <%
    next
    Method5 = StopClock
    Response.Clear
End Function



Function Method6(tCount)
    StartClock
    response.Write "<h1>LOOP WITHOUT MIX</h1>"
    for i = 1 to tCount
      response.Write "<select name=option" & i & ">" &_    
        "<option>1</option>" &_
        "<option>2</option>" &_
        "<option>3</option>" &_
       "</select>"     
    next
    Method6 = StopClock
    Response.Clear
End Function


'------------------ Clock Functions -------------
' Usually these would be in a language that has
' better millisecond resolution, but shouldn't
' be necessary in this case
Function StartClock()
    s_time = timer
End Function

Function StopClock()
    StopClock = timer - s_time
End Function

'------------------ RunTest Function ------------
' Allows us to make one call and run any test
Function RunTest(testNum, loopCount)
    Select Case testNum
        Case "1"
            Runtest = Method1(loopCount)
        Case "2"
            Runtest = Method2(loopCount)
        Case "3"
            Runtest = Method3(loopCount)
        Case "4"
            Runtest = Method4(loopCount)
        Case "5"
            Runtest = Method5(loopCount)
        Case "6"
            Runtest = Method6(loopCount)
        Case Else
            Runtest = -1
    End Select
End Function
%>

In order to test this on your own server you must create 2 include files:
option.asp
Code:
<option>1</option>
<option>2</option>
<option>3</option>


option.js
Code:
document.write('<option>1</option><option>2</option><option>3</option>');



Method#4 is an attempt to estimate the speed of a recordset approach (You read the records in an array).



The result on my W2K 1.7 P4 256 Mb RAM desktop:

Benchmark Results
Method Name Avg Low High
Method #1 Options in option.asp 0.036 0.031 0.047
Method #2 Options in option.js 0.041 0.031 0.047
Method #3 Options in UDF 0.061 0.047 0.063
Method #4 Options in array 0.114 0.109 0.125
Method #5 Loop with mix 0.017 0.016 0.031
Method #6 Loop without mix 0.061 0.047 0.063

Times are averages from 10 runs of 10000 loops for each method.

... and that surprises me a lot! Again i can start changed "some" codelines!

ttmug.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top