Im doing a page in .asp and im having this problem ... I have some variable constants
MENU_01="FICHEROS"
MENU_02="OPTIONS"
MENU_03="USERS"
etc ... what I need to do its a cicle where I can print each variable value by refering it by string ...
Dim i
For i=1 to CANTMENUS
Response.Write "<li>"& MENU_0 & i; & "</li>"
Next
Something like that (obviously that do not work) I know the variable name start in MENU_0 and I want dinamicly add the next value to the variable name (the "i" value)
Its this posible to do ??
thanks for all.
Can be done with Response.Write "<li>"& Eval("MENU_0" & i) & "</li>" but using eval is not recommended because of it's not a reliable method. I'd suggest the use of dictionary object.
Set MENU = Server.CreateObject("Scripting.Dictionary")
MENU.Add "01", "FICHEROS"
MENU.Add "02", "OPTIONS"
MENU.Add "03", "USERS"
For Each Item In MENU.Items
Response.Write "<li>" & Item & "</li>"
Next
Another option is using arrays.
In fact I found a way to do this without a set or array! By using eval() func
eval("MENU_"&MENUNUMBER)
MENUNUMBER its the iteration variable and the variables with the values I want are MENU_01 MENU_02 MENU_03 etc ... and with eval("MENU_01") I get the value I need!
Thanks for all.
Related
In the code block below I am wishing to use a variable in a xxxx.Rows.Count line.
Specifically, I'm wanting to take the "R1200s" portion and increment it to R1201s, R1202s, R1203s and so on as the For-Next loop executes. I, of course, already have the DataTable()'s portion defined... no problem there. The code works fine if I write out every single instance but I'd like to shorten up into a simpler block of code. I've been able to use "i" in every other control with success but getting hung up on only this portion.
I've tried something like ("R120" & i & "s"), along with many variations of this but, can find no solution that works.
Any help, or a point in the right direction, is appreciated.
For i = 1 To 8
Dim Ai As Label = DirectCast(Page.FindControl("A" & ID.ToString()), Label)
Dim Si As Hyperlink = DirectCast(Page.FindControl("S" & ID.ToString()), Hyperlink)
If R1200s.Rows.Count < Slots.Rows(0)(Ri).ToString() +1 Then
Ai.Style.Add("background-color","#000000")
Si.ImageUrl = "images/" & ID & ".png"
Si.NavigateUrl = "step3.aspx?Time=" & ID & "&Date=" & Request.QueryString("Date") & "&Day=" & Request.QueryString("Day") & "&Entry=" & Request.QueryString("Entry")
End If
Next
I am having the following issue:
I am using an application that allows users to concatenate text to build a URL that passes parameters to an ASP page via GET method, i.e. something like:
http://myhostname/process.asp?param1=value1¶m2=value2
Problem is value1 and value2 can contain the ampersand symbol, which is not interpreted as a text character.
The most popular solution to this issue is to encode the URL, which is not an option for me because I cannot modify the program that builds the URL. I can modify the process.asp page, but not the program that concatenates the text fields and builds the URL.
Things I've tried to search for are:
How to encode a URL using javascript directly in the browser
How to change IIS default behaviour when reading an &
Alternative ways to pass parameters, i.e. something like passing them as a single string of characters separated with pipes
Hope you can give me some guidance.
You can read the entire query string and parse it yourself, like this:
q = Request.QueryString
a = Split(q, "=")
i = 1
For Each s In a
If i mod 2 = 0 Then
If InStr(s, "&") <> InStrRev(s, "&") Then
Response.Write "Value: " & Left(s, InStrRev(s, "&") - 1) & "<br/>"
hidingParam = Right(s, Len(s) - InStrRev(s, "&"))
Response.Write "PAramName: " & hidingParam & "<br/>"
i = i + 1
Else
Response.Write "Value: " & s & "<br/>"
End If
Else
Response.Write "PAramName: " & s & "<br/>"
End If
i = i + 1
Next
Result:
URL: ...?Q=abc&def&P=123 produces
PAramName: Q Value: abc&def PAramName: P Value: 123
Note that this is less than robust. I am only illustrating my idea. I didn't test with no &.
It also doens't handle multiple "=" characters (if that's a possiblity as well).
If there are 2 (or more) ampersands in-between the equals, then only the last one is a parameter separator. So, using your URL above, and assuming that value1 = "abc&def", and value2 = "123", then the URL will look like:
http://myhostname/process.asp?param1=abc&def¶m2=123
Notice there's 2 ampersands in-between the 2 equals. The last one will be your parameter separator, the rest are part of the value. And any ampersands after the last equals are also part of the value.
You'll have to dissect the incoming URL and apply the appropriate logic.
HI, using vs2008 and building a web app. On a asp page called blackjack.aspx, I have four labels with id of lbBJTStatusP1 lbBJTStatusP2 lbBJTStatusP3 lbBJTStatusP4.
I want to address those labels in a single sub by casting the casting two strings into the control name, so that string lbBJTStatusP & "1" would refer to lbBJTStatusP1.This is done on the code behind page.
So far I have tried this but with no success. boxct refers to either "1" "2" "3" or "4".
DirectCast(blackjack.Controls.Find("lbBJTStatusP" & boxct, True)(0), Label).BackColor = stoodcolor
Can it be done and if so how. Thanks for all and any help.
You can't "cast" a string to a specific instance of a control.
What you can do is use FindControl: that accepts a string, searches (one level deep, not more) for a control with that name and returns it. The method returns a Control, so you might need to cast it to Label.
I have labels named lblqu01ex - lblqu10ex. I set the text value through coding as follows.
for i = 1 to 10
ex = "lbl" & IIf(i = 10, "qu10", "qu0" & i) & "ex"
DirectCast(FindControl(ex), Label).Text = 2*100/i
next
Its work.
Having an issue with type conversion in ASP classic.
heres my code:
Set trainingCost = Server.CreateObject("ADODB.Recordset")
strSQL3 = "SELECT cost1 FROM tblMain WHERE (Booked = 'Booked') AND (Paid IS NULL) AND (PaidDate BETWEEN '01/04/" & startyear & "' AND '31/03/" & endyear & "')"
trainingCost.Open strSQL3, Connection
trainingCost.movefirst
totalTrainCost = 0
do while not trainingCost.eof
trainCost = trainingCost("cost1")
If NOT isNull(trainCost) then
trainCostStr = CStr(trainCost)
trainCostStr = Replace(trainCostStr, "£", "")
trainCostStr = Replace(trainCostStr, ",", "")
totalTrainCost = totalTrainCost + CInt(trainCostStr)
end if
trainingCost.movenext
loop
trainingCost.close
when I run this I get the following error:
Microsoft VBScript runtime (0x800A000D)
Type mismatch: 'CInt'
/systems/RFT/v1.2/Extract.asp, line 43
which is "totalTrainCost = totalTrainCost + CInt(trainCostStr)"
Im guessing that the problem is to do with the String value being uncastable to Int in which case is there any way to catch this error? I havent worked with asp classic much so any help would be usefull
cheers
-EDIT-
the type of column cost1 is String as it may contain a number or a sequence of chars eg £10.00 or TBC
You have a couple of choices. You can be proactive by checking ahead of time whether the value is numeric using the IsNumeric function:
If IsNumeric(trainCostStr) Then
totalTrainCost = totalTrainCost + CInt(trainCostStr)
Else
' Do something appropriate
End If
...or you can be reactive by using error catching; in Classic ASP probably easiest to define a function and use On Error Resume Next:
Function ConvertToInt(val)
On Error Resume Next
ConvertToInt = CInt(val)
If Err.Number <> 0 Then
ConvertToInt = Empty
Err.Clear
End If
End Function
Or return 0 or Null or whatever suits you, then use it in your trainCost code.
Note that CInt expects an integer and will stop at the first non-digit, so "123.45" comes back as 123. Look at the other conversions, CDouble, CCur, etc.
Rather than casting to a string, why not use CCur (Cast as Currency) so that your commas and any currency symbols (I think) are effectively ignored while doing arithmetic operations?
Potentially solving the wrong problem, depends on the type of Cost1 within the database but the code is looping through the records to generate a total.
strSQL3 = "SELECT sum(cost1) FROM tblMain WHERE (Booked = 'Booked') AND (Paid IS NULL) AND (PaidDate BETWEEN '01/04/" & startyear & "' AND '31/03/" & endyear & "')"
trainingCost.Open strSQL3, Connection
etc and just read off the value as a total.
I don't see why the RS is being looped to generate a sum when the database can do that work for you. All the conversion work it has generated just looks artifical.
Heh heh. Classic ASP. You have my pity :) Anyway,
On error resume next
And then on the next line, check that it worked.
Though maybe you want CDouble. Is that a function? I can't remember.
I want to be able to do:
For Each thing In things
End For
CLASSIC ASP - NOT .NET!
Something like this?
dim cars(2),x
cars(0)="Volvo"
cars(1)="Saab"
cars(2)="BMW"
For Each x in cars
response.write(x & "<br />")
Next
See www.w3schools.com.
If you want to associate keys and values use a dictionary object instead:
Dim objDictionary
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "Name", "Scott"
objDictionary.Add "Age", "20"
if objDictionary.Exists("Name") then
' Do something
else
' Do something else
end if
Whatever your [things] are need to be written outside of VBScript.
In VB6, you can write a Custom Collection class, then you'll need to compile to an ActiveX DLL and register it on your webserver to access it.
The closest you are going to get is using a Dictionary (as mentioned by Pacifika)
Dim objDictionary
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.CompareMode = vbTextCompare 'makes the keys case insensitive'
objDictionary.Add "Name", "Scott"
objDictionary.Add "Age", "20"
But I loop through my dictionaries like a collection
For Each Entry In objDictionary
Response.write objDictionary(Entry) & "<br />"
Next
You can loop through the entire dictionary this way writing out the values which would look like this:
Scott
20
You can also do this
For Each Entry In objDictionary
Response.write Entry & ": " & objDictionary(Entry) & "<br />"
Next
Which would produce
Name: Scott
Age: 20
One approach I've used before is to use a property of the collection that returns an array, which can be iterated over.
Class MyCollection
Public Property Get Items
Items = ReturnItemsAsAnArray()
End Property
...
End Class
Iterate like:
Set things = New MyCollection
For Each thing in things.Items
...
Next
As Brett said, its better to use a vb component to create collections. Dictionary objects are not very commonly used in ASP unless for specific need based applications.
Be VERY carefully on using VB Script Dictionary Object!
Just discover this "autovivication" thing, native on this object: http://en.wikipedia.org/wiki/Autovivification
So, when you need to compare values, NEVER use a boolen comparison like:
If objDic.Item("varName") <> "" Then...
This will automatically add the key "varName" to the dictionary (if it doesn't exist, with an empty value) , in order to carry on evaluating the boolean expression.
If needed, use instead If objDic.Exists("varName").
Just spend a few days knocking walls, with this Microsoft "feature"...
vbscript-dictionary-object-creating-a-key-which-never-existed-but-present-in-another-object