Adam
Adam

Reputation: 9049

ASP my simple if statement is not working

I'm doing a simple a simple html site that I wrote which needed some php, however the server does not have php, only asp. Now I have to learn a bit of asp.

<li><a id="main-link" <% If selected = "main" Then Repsonse.Write("class='selected'") End If %> href="/home/">main</a></li>

What is wrong with mode code above? I read some tutorials and that what I came up with.

Upvotes: 2

Views: 303

Answers (2)

AnthonyWJones
AnthonyWJones

Reputation: 189457

You have a typo as @derekaug has already pointed out. However by way of education here is how I would do it. In an early section of the page where you would put general helper code (and probably where the selected variable gets assigned):

 Function GetLinkClass(link)
     If link = selected Then
         GetLinkClass = "class=""selected"""
     Else
         GetLinkClass = ""
     End If
 End Function

then your links looks like this

 <li><a id="main-link" <%=GetLinkClass("main")%> href="/home/">main</a></li>     

you'll clearly have more than one of these and this would look much tidier. Its always a good idea to keep the amount of actual code dispersed in HTML elements to the absolute minimum by using functions to contain any logic.

Upvotes: 1

derekaug
derekaug

Reputation: 2145

You have a typo in your code for Response.Write, try this:

<li><a id="main-link" <% If selected = "main" Then Response.Write("class='selected'") End If %> href="/home/">main</a></li>

Upvotes: 0

Related Questions