how to incorporate a c# variable into parameters of javascript function
I am trying to put a path kept in a string variable (named "ruta") into the parameters of the swfobject.embedSWF
funtion but I don't know how to incorporate开发者_运维技巧 a c# code into javascript code. Can someone help me please?? thanks!!!!!
<%TarjetaPL tarjetaPl = null;
string ruta = null;
if (Session[Constantes.TarjetaSeleccionada] != null)
{
tarjetaPl = new TarjetaPL((Tarjeta)Session[Constantes.TarjetaSeleccionada]);
ruta = "../../content/images/" + tarjetaPl.TipoDeTarjeta.Banner;
}%>
<script type="text/javascript">
swfobject.embedSWF((HERE COMES THE PATH KEPT IN THE VARIABLE "ruta"), "flashBanner", "300", "120", "9.0.0");
</script>
The problem is that the code doesn't even recognize the " <% %> " tag to incorporate c# on it!
This will output a value to the location you specify.
You want something like:
<script type="text/javascript">
swfobject.embedSWF(<%=ruta%>, "flashBanner", "300", "120", "9.0.0");
</script>
Sorry it's been a while since I did web-based C# stuff
In your code-behind file (assuming you have one), place the following code:
protected string Ruta
{
get
{
TarjetaPL tarjetaPl = null;
string ruta = null;
if (Session[Constantes.TarjetaSeleccionada] != null)
{
tarjetaPl = new TarjetaPL((Tarjeta)Session[Constantes.TarjetaSeleccionada]);
ruta = "../../content/images/" + tarjetaPl.TipoDeTarjeta.Banner;
}
return ruta;
}
}
And in your .aspx page, place this code:
<script type="text/javascript">
swfobject.embedSWF(<%=Ruta %>, "flashBanner", "300", "120", "9.0.0");
</script>
It needs to be treated differently within a <script/>
block. Fix it by wrapping your <%=%> blocks with single or double quotes, I tend to use single because sometimes you need to use double quotes within the c# block and it will not be parsed otherwise.
<%
int number = 1;
string str = "hi hi";
%>
<script type="text/javascript">
alert('<%=number%>');
alert('<%=str%>');
</script>
精彩评论