Wim
Wim

Reputation: 213

Using variables in C# tags in html, using asp.net

I was wondering if it was possible to define and use a variable in your html using C# tags.

So for instance

<% String var = "simpleString"; %>
<link rel="stylesheet" type="text/css" href="style.css?v=<%Response.Write(var);%>" />

I have been trying to get something like this to work. When I write it like

<% String var = "pineapples"; Response.Write(var);%>

it works, but when I write them in separate tags it doesn't seem to read it as a variable.

Upvotes: 2

Views: 4449

Answers (1)

hutchonoid
hutchonoid

Reputation: 33326

var is a keyword in c# used to implicitly type a variable.

String is explicitly typed.

You have used both in your example:

String var = "simpleString";

You could change it to:

String var1 = "simpleString";

using your desired variable name above.

<% String var1 = "simpleString"; %>
<link rel="stylesheet" type="text/css" href="style.css?v=<%=var1%>" />

Also note that from mark-up you can use <%= %> to output rather than Response.Write.

Upvotes: 3

Related Questions