Link to home
Start Free TrialLog in
Avatar of KenAdney
KenAdney

asked on

running ASP code from an onclick event

I'm embarrased I don't know the answer to this but...

I'm retrieving data from a SQL table and displaying it as a table in the browser.  Now I want to give the user the option to download the table as an Excel file.

The code for the download is:

Response.AddHeader "Content-Disposition", "attachment;filename=invoice.xls"
Response.ContentType = "application/vnd.ms-excel"

which works OK but how do I call this from a button?

I assume something like:

<INPUT type="button" value="Download this file" onclick="downloadit()">

Do I put the ASP code inside a function downloadit?
Do I create a variable with a value and then run the response commands with an IF...Then?

What's the best way to do this?  Please be explicit, I'm sort of slow today...
Avatar of peh803
peh803
Flag of United States of America image

Something like this:
<%
if len(request("downloadit"))>0 then
   '''Put your code here to download the excel file....
end if
%>
<form name="downloadform">
Click the button to download the data in excel format!<BR>
<input type="submit" name="downloadIt">
</form>


Regards,
peh803
Avatar of dfu23
dfu23

The function downloadit() is a client side javascript function which will either need to submit the form to an ASP page (usually the same ASP page that it is on but not always the case) or it will need to point the browser to the ASP page that will begin the download.

So maybe,

function downloadit() {
    document.forms[0].action = "myPage.asp?download=1";
    document.forms[0].submit();
}

This way the ASP page can check for the QueryString of download as a switch to begin the download ... or maybe have a separate ASP page:

function downloadit() {
    document.location.href = "download.asp";
}
SOLUTION
Avatar of justinbillig
justinbillig

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Actually if you want to do it as a remote script you could use a javascript remote procedure call which doesn't have to be IE specific ...
dfu could you elaborate more on what you mean by javascript remote procedure call. A link if you have one also please
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i forgot to add the download button .....
...... as given above
<BODY>
<Form name="frmDownload" method=post>
     <input type=submit name="butDownload" value="Download">

... formatting of the data
</Form>
</BODY>
</HTML
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KenAdney

ASKER

Thanks everyone, I've upped the points now that I see the complexity of what we're dealing with.

Here's the whole code for the page (I've some comments at the bottom):

<%
Dim pageName
pageName = "exceldownload.asp"
bill_month = Trim(Request.form("bill_month"))

If bill_month = "" THEN
%>
<h4>Select a month to retrieve the information</h4>
<% Call showSelectMonthForm(-6,6) 'Prev-#-Months to Future-#-Months. Use '3,3' for a 7 months span including current month%>
<hr>

<%
ELSE
security()
Dim DataConn
Dim contractor, sub_contractor, count_psa, count_brief_office, count_commed, count_intake_office, count_intake_home, count_app_office, count_app_home, count_group


      'get the contractor name based on the login ID
      openDb()
      Set rs = DataConn.Execute ("SELECT sub_contractor, contractor FROM contractors WHERE userid = '" & loginid &"'")
      sub_contractor = rs("sub_contractor")
      contractor = rs("contractor")
      closeDb()

      'retrieve the data for the contractor and specified bill_month
      openDB()
            sql1 = ("SELECT * FROM sub_bill_new WHERE bill_month = '" & bill_month & "' AND contractor = '" & contractor &"' ORDER BY sub_contractor" )
            Set rs1 = Server.CreateObject("ADODB.Recordset")
            rs1.Open sql1, DataConn, 1, 3

If rs1("bill_month") = "" Then die("There aren't any Activity Reports entered.")             
                  count_psa = rs1("count_psa")
                  count_brief_office = rs1("count_brief_office")
                  count_commed = rs1("count_commed")
                  count_intake_office = rs1("count_intake_office")
                  count_intake_home = rs1("count_intake_home")
                  count_app_office = rs1("count_app_office")
                  count_app_home = rs1("count_app_home")
                  count_group = rs1("count_group")
%>

<%

' create the excel file out of a table of the data
Response.AddHeader "Content-Disposition", "attachment;filename=invoice.xls"
Response.ContentType = "application/vnd.ms-excel"

%>




<table width="90%" border="2" bordercolor="green">
<tr>
<th width="70%"><b>Bill Month</b></th>
<th width="70%"><b>Sub-contractor</b></th>
<th width="15%"><b>Brief Office</b></th>
<th width="15%"><b>Intake Office</b></th>
<th width="15%"><b>Intake Home</b></th>
<th width="15%"><b>App Office</b></th>
<th width="15%"><b>App Home</b></th>
<th width="15%"><b>Group</b></th>
<th width="15%"><b>Comm Ed</b></th>
<th width="15%"><b>PSA</b></th>
</tr>
<%
While Not rs1.EOF
%>
<tr>
<td>'<%=bill_month%></td>
<td><%=sub_contractor%></td>
<td><%=count_brief_office%></td>
<td><%=count_intake_office%></td>
<td><%=count_intake_home%></td>
<td><%=count_app_office%></td>
<td><%=count_app_home%></td>
<td><%=count_group%></td>
<td><%=count_commed%></td>
<td><%=count_psa%></td>
</tr>
<%
rs1.MoveNext
Wend
%>
</table>

<%End If%>

<%
Function showSelectMonthForm(prevMonths,futureMonths)
      ' This function creates a small html form that allows the user to select
      '      the BillingContract month to be displayed.
      ' In turn, the values selected from this form will let us auto-fill much of the
      '      rest of the form on this page.
      ' We will get months from 'prefMonths' months back through 'futureMonths' months ahead of the present month,
      '      this is changes by the parameters set when calling this function
      %>
      <form action="<%= pageName %>" method="post" name="selectMonthForm">
            <select name="bill_month">
            <%
            For i = prevMonths To futureMonths
                  
                  'as we loop through, get the date for each month
                  newDate = DateAdd("M",i,Date)
                  
                  ' the value of 'm' is the MONTH portion of each date
                  m = Month(newDate)

                  'add a zero to the front of the month if it is only one digit
                  If Len(m) < 2 Then m = 0 & m

                  ' the value of 'y' is the YEAR portion of each date, it is probably in a 4-digit format, ie '2004'
                  y = Year(newDate)

                  ' change the 4-digit year to a 2-digit year
                  If Len(y) = 4 Then shortYear = Right(y,2)

                  'add the word "SELECTED" if this is the current month, so the default view will be of the current month
                  If DateDiff("M",newDate,Date) = 0 Then isCurrentMonth = " SELECTED" Else isCurrentMonth = ""

                  'however, if this form has already been used, default to the last selected month, not the current month
                  If Request.form("bill_month") <> "" Then
                        bill_month = Request.form("bill_month") ' is in the form of MM-YY, i.e., '09-04'
                        If DateDiff("M",newDate,bill_month) = 0 Then isCurrentMonth = " SELECTED" Else isCurrentMonth = ""
                  End If

                  'print it all out. Note the 'value' uses the shortYear format, but the long year (y) is displayed for the user.
                  %>
                  <option value="<%= m %>-<%= shortYear %>"<%= isCurrentMonth %>><%= m %>-<%= y %></option>
            <% Next %>
            </select>
            <input type="submit" value="Get data from this billing/contract month">
      </form>
      <%
End Function 'showSelectMonthForm()
      %>

me again...there's some functions in a secured logon script here that you won't see (including the dataconn, openDB, making a cookie of the logonID, etc.).  

it makes sense to me that if the page is already rendered, it can't be downloaded directly.  Also that a form inside a form isn't going to work.  It looks like a javascript RPC to a new page (perhaps saving all the fields as session variables so they can be used again) and this new page doing the download.  Waddya think?
any specific reson for not trying what i suggested .. call the same page when u want to download .. but this time change the contenttype ...
Other than me being stupid as a sack full of hammers, no.

Turns out, at least with IE, the content type didn't need to change and a form inside a form worked.

Here's what works (although with the hidden fields, renaming bill_month and using the flag to strip the button out of the Excel file, it's a little messy) but at least it is all ASP:

<%
Dim pageName
pageName = "exceldownload.asp"
Dim flag

Flag=True

If request.QueryString("getit")="1" then
      ' create the excel file out of a table of the data
      Response.AddHeader "Content-Disposition", "attachment;filename=invoice.xls"
      Response.ContentType = "application/vnd.ms-excel"

      bill_month = Request.QueryString("bill_month2")
      

      Flag=False
Else
      bill_month = Request.form("bill_month")
End if



If bill_month = "" THEN
%>
<h4>Select a month to retrieve the information</h4>
<% Call showSelectMonthForm(-6,6) 'Prev-#-Months to Future-#-Months. Use '3,3' for a 7 months span including current month


%>

<hr>

<%
ELSE
security()
Dim DataConn
Dim contractor, sub_contractor, count_psa, count_brief_office, count_commed, count_intake_office, count_intake_home, count_app_office, count_app_home, count_group


      'get the contractor name based on the login ID
      openDb()
      Set rs = DataConn.Execute ("SELECT sub_contractor, contractor FROM contractors WHERE userid = '" & loginid &"'")
      sub_contractor = rs("sub_contractor")
      contractor = rs("contractor")
      closeDb()

      'retrieve the data for the contractor and specified bill_month
      openDB()
            sql1 = ("SELECT * FROM sub_bill_new WHERE bill_month = '" & bill_month & "' AND contractor = '" & contractor &"' ORDER BY sub_contractor" )
            Set rs1 = Server.CreateObject("ADODB.Recordset")
            rs1.Open sql1, DataConn, 1, 3

If bill_month = "" Then die("There aren't any Activity Reports entered.")             
                  count_psa = rs1("count_psa")
                  count_brief_office = rs1("count_brief_office")
                  count_commed = rs1("count_commed")
                  count_intake_office = rs1("count_intake_office")
                  count_intake_home = rs1("count_intake_home")
                  count_app_office = rs1("count_app_office")
                  count_app_home = rs1("count_app_home")
                  count_group = rs1("count_group")
%>

<table width="90%" border="2" bordercolor="green">
<tr>
<th width="70%"><b>Bill Month</b></th>
<th width="70%"><b>Sub-contractor</b></th>
<th width="15%"><b>Brief Office</b></th>
<th width="15%"><b>Intake Office</b></th>
<th width="15%"><b>Intake Home</b></th>
<th width="15%"><b>App Office</b></th>
<th width="15%"><b>App Home</b></th>
<th width="15%"><b>Group</b></th>
<th width="15%"><b>Comm Ed</b></th>
<th width="15%"><b>PSA</b></th>
</tr>
<%
'While Not rs1.EOF
%>
<tr>
<td>'<%=bill_month%></td>
<td><%=sub_contractor%></td>
<td><%=count_brief_office%></td>
<td><%=count_intake_office%></td>
<td><%=count_intake_home%></td>
<td><%=count_app_office%></td>
<td><%=count_app_home%></td>
<td><%=count_group%></td>
<td><%=count_commed%></td>
<td><%=count_psa%></td>
</tr>
<%
'rs1.MoveNext
'Wend
%>
</table>

<%If flag=True Then%>

<form name="downloadform" method="get">
Click the button to download the data in excel format!<BR>
<input type="hidden" name="getit" value="1">
<input type="hidden" name="bill_month2" value="<%=bill_month%>">
<input type="submit" name="downloadIt" value="Download the file">
</form>
<%End If%>

good .. so that works ...
oh you just mean remote scripting ... ok yeah i know htat