Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 17th, 2003, 05:52 AM
Registered User
 
Join Date: Jul 2003
Location: gorey, , Ireland.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default pass javascript value to label

I need to pass value from a javascript function to a label in an asp/aspx page.
I cannot find information on this anywhere,
has this been done before

can anybody help
 
Old July 17th, 2003, 06:10 AM
Authorized User
 
Join Date: Jul 2003
Location: Houten, Utrecht, Netherlands.
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to bosboom
Default

The only way to do this is to load a .asp page in a browser, so i suggest to use a <iframe> or hidden frame in your page and load the page which updates the value with javascript like so:

hiddenframe.location.href="yourpage.asp?value=your value"

GreetZ

Bosboom

 
Old July 17th, 2003, 07:52 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

If you mean change an asp:label control from client-side JavaScript without posting then you need to know the clien-side id of the control. A label control is only an HTML span element. I believe you can set this server-side using clientID property.
Then in your client-side JavaScript
Code:
  function setLabelText(ID, Text)
  {
    document.getElementById(ID).innerHTML = Text;
  }
Then call this function from somewhere, assuming id is "ctrl1"::
Code:
  setLabelText("ctrl1", "Hello, World.");
--

Joe
 
Old August 30th, 2006, 02:29 PM
Registered User
 
Join Date: Aug 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

that was helpful...

it works!!!

 
Old July 3rd, 2008, 12:55 AM
Registered User
 
Join Date: Jul 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

how do i access the innerText or the innerHTML property of a label using the C# code behind ? please let me know

 
Old July 3rd, 2008, 03:45 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Don't reply to very old posts with an essentially unrelated question.

And in any case, you should have posted this in the C# forum.

But whatever...

The simple answer: YOU DON'T.

C# code *ON THE SERVER* can *ONLY* see information that is passed to it FROM A FORM on the client. So you would have to use JavaScript in the HTML page to copy the innerHTML value to a form field (presumably, a HIDDEN form field) so that the server could the read it.

Incidentally, innerText is not supported in all browsers. You probably should stick with innerHTML.
 
Old July 4th, 2008, 03:06 AM
Registered User
 
Join Date: Jul 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i'm new to this forum. I'm a bit annoyed to see this kinda rude reply !! Thanks for the reply anyways ... and this was an essentially related question.

 
Old July 5th, 2008, 04:04 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Even if you saw your question as related to the old thread, you would get a much better reponse (most of the time) if you posted in the right forum.

JavaScript truly is just for client-side use. And it really has nothing (directly) to do with C# programming or with server-side code, in general.

If you don't yet understand the difference between client and server, that's okay. But now is the time to learn the difference, before you go off on a long journey down the wrong path. I remember a person who wrote an incredibly long and complex set of ASP (pre-ASP.NET) pages that freely mixed JavaScript in the browser and server-side coding. And he managed to make it all work on his own personal machine. But when he went to deploy it to the 'net, of course it fell flat on its face. Because he didn't understand that ONLY on his local machine were the browser and the server the same machine, with the same resources. He had to throw away weeks and weeks of work and start all over.

So if I seem over-emphatic, sorry. But I'm really just trying to save you time and effort. Save you hours if you are lucky, days if you are not.

Anyway, from my viewpoint your question is utterly unrelated to the old thread. How you get information from server to browser is, indeed, completely different from how you get information from browser to server. The server can easily put anything to the browser any way it wants to. Dirt simple. The browser can *ONLY* influence the server by sending data back to the server by means of a query string or post data. (With ASP.NET--and excepting for Ajax--99% of the time post data is used, but that's mainly because of the standard controls in the MS "toolkit".)

So... If I irritated you, it wasn't on purpose. But if I irritated you into thinking about how client/server really works, then it was worthwhile, anyway.




Similar Threads
Thread Thread Starter Forum Replies Last Post
how to pass javascript variable to xsl eruditionist XSLT 4 September 8th, 2008 08:37 AM
Change ASP.NET label color using Javascript shalan99 ASP.NET 2.0 Professional 4 May 22nd, 2007 02:09 AM
How To Pass Javascript variable to vbscript pbcatan Classic ASP Databases 1 December 18th, 2006 08:42 AM
How Do I Pass 2 Variables in Javascript Lucy Javascript 2 December 13th, 2003 11:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.