473,395 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Passing C# variable to javascript

I am trying to display an image on my page by using "img src= hyperlink".
the hyperlink is stored in a c# variable. but i seem to have the syntax
mixed up.

This passing the image directly works e.g.
objCell.Text = "<img src=\"image.gif\">";

but using a variable does not seen to work.

String imageLocation="image.gif";

objCell.Text = "<img src=imageLocation >";
i am having to use a variable because the statement will be used within a
loop

Thanks
Nov 17 '05 #1
2 8746
The variable name in your example is included inside quotes and will thus be
interpreted as a string literal, NOT a variable name. Try this:

objCell.Text = string.Format("<img src=\"{0}\">", imageLocation.ToString());

Of course, if imageLocation is already a string you can omit ToString().

I hope this helps.

--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.

"jbatty" <ja***@batty.com> wrote in message
news:eb**************@TK2MSFTNGP09.phx.gbl...
I am trying to display an image on my page by using "img src= hyperlink".
the hyperlink is stored in a c# variable. but i seem to have the syntax
mixed up.

This passing the image directly works e.g.
objCell.Text = "<img src=\"image.gif\">";

but using a variable does not seen to work.

String imageLocation="image.gif";

objCell.Text = "<img src=imageLocation >";
i am having to use a variable because the statement will be used within a
loop

Thanks

Nov 17 '05 #2
Thank you very much!

"Kai Brinkmann [MSFT]" <ka******@online.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
The variable name in your example is included inside quotes and will thus
be interpreted as a string literal, NOT a variable name. Try this:

objCell.Text = string.Format("<img src=\"{0}\">",
imageLocation.ToString());

Of course, if imageLocation is already a string you can omit ToString().

I hope this helps.

--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no
rights.

"jbatty" <ja***@batty.com> wrote in message
news:eb**************@TK2MSFTNGP09.phx.gbl...
I am trying to display an image on my page by using "img src= hyperlink".
the hyperlink is stored in a c# variable. but i seem to have the syntax
mixed up.

This passing the image directly works e.g.
objCell.Text = "<img src=\"image.gif\">";

but using a variable does not seen to work.

String imageLocation="image.gif";

objCell.Text = "<img src=imageLocation >";
i am having to use a variable because the statement will be used within a
loop

Thanks


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jonas Daunoravicius | last post by:
The subject pretty much sums up what I need to do. Here is what I have so far, but still can't figure out how to get it working: <script language="javascript" type="text/javascript"> function...
1
by: Patrice | last post by:
Hi, I'm trying to use a javascript function passing a vb variable. I don't understand why it doesn't work. Can someone help me? Thanks in advance. Here is my javascript function used to...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
2
by: RootShell | last post by:
Hello First of all i know that PHP is a server side, and JavaScript is a Client side programming language, but i need to know if there is any way i can add the variable "countDownInterval" from...
1
by: cirillo_curiosone | last post by:
Hi, i'm new to javascript. I started studing it on the web few weeks ago, but still haven't been able to solve one big problem: HOT TO PASS VALUES FROM A SCRIPT VARIABLE TO A CHILD HTML...
2
by: brianwmunz | last post by:
Hi, I'm having a problem passing a variable through a URL because the variable is supposed to hold a URL that has a variable of its own. Here is an idea of what I'm trying to do: href="javascript:...
6
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an...
5
by: sfeher | last post by:
Hi All, I need to call a function(loaded with appendChild) for which I have the name as a string. .... var fnName = 'fn1'; var call = fnName + '('+ param +' )'; eval(call);
2
satterfieldben
by: satterfieldben | last post by:
I have a newbie question about passing variables between functions. I am wanting to use a drop down box to select a value. Then base on which was selected, it would create a variable and I would...
5
by: aelred | last post by:
I have a web page where a member can open up a chat window (child window) with another member. - From there the member can also navigate to other web pages. - From other pages in the site, they...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.