Ok folks, I am trying to write a line of code in Classic ASP for a button that will trigger it to go to another page with a querystring variable. However I am having difficulties getting this to work.
Here is my code so far:
response.write "<td valign='top'><input type='button' name='cmdUpdateInfo' value='Update Info' tabindex='7' onclick='window.location=updatecustomer.asp?UpdateID=""'" & rs("ID") & "'""/></td>" & vbCrLf
When this line is processed I get the following error on my ASP processor: Error Type: (0x80020009) Exception occurred. /ls_internal/newinvoice.asp, line 325
line 325 is the line of code above. I know this is something simple, but for the life of me, I cannot seem to figure it out.
Thank you in advance.
For one you should use double quotes for html attributes and single for javascript so
onclick=window.location=updatecustomer.asp?UpdateID=""'" & rs("ID") & "'"" />
should be
onclick=""window.location='updatecustomer.asp?UpdateID=" & rs("ID") & "'"" />
Other than that you should output html directly, without having asp handle it as a string to print.
What i mean is that instead of the whole response.write .....
line you could do
%><td valign="top"><input type="button" name="cmdUpdateInfo" value="Update Info" tabindex="7" onclick="window.location='updatecustomer.asp?UpdateID=<%=rs("ID")%>'"/></td>
<%
As you've currently written your line of code it will translate into
<td valign='top'><input type='button' name='cmdUpdateInfo' value='Update Info'
tabindex='7' onclick='window.location=updatecustomer.asp?UpdateID="'999'"/></td>
Assuming that the rs("id") contains the value 999. There is nothing wrong with that line of code, first thing I would try doing is comment out the rs("id") element and see if the page runs ok.
If it doesn't then I would check further up your page - are you sure that your SQL query is returning anything? I wonder if your rs is empty (not a NULL value, I mean no records were returned) In which case you need to add an IF NOT rs.eof THEN
condition to your code.
Personally I would follow Gaby aka G.Petrioli's recommendation above to convert your HTML attributes into double quotes, and JavaScript to single.
As this come inside a loop I prefer to not break out of the ASP code and instead use kind of "template":
Dim cellTemplate
cellTemplate = "<td valign=""top""><button type=""button"" name=""cmdUpdateInfo"" tabindex=""7"" onclick=""window.location = 'updatecustomer.asp?UpdateID=$id';"">Update Info</button></td>"
Do Until rs.EOF
response.write Replace(cellTemplate, "$id", rs("ID")) & vbCrLf
rs.MoveNext
Loop
This is more elegant and won't create spaghetti code, plus you better use ordinary button and not <input>
element.
User contributions licensed under CC BY-SA 3.0