 |
| 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
|
|
|
|

July 17th, 2003, 05:52 AM
|
|
Registered User
|
|
Join Date: Jul 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

July 17th, 2003, 06:10 AM
|
|
Authorized User
|
|
Join Date: Jul 2003
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

July 17th, 2003, 07:52 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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
|
|

August 30th, 2006, 02:29 PM
|
|
Registered User
|
|
Join Date: Aug 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
that was helpful...
it works!!!
|
|

July 3rd, 2008, 12:55 AM
|
|
Registered User
|
|
Join Date: Jul 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
how do i access the innerText or the innerHTML property of a label using the C# code behind ? please let me know
|
|

July 3rd, 2008, 03:45 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
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.
|
|

July 4th, 2008, 03:06 AM
|
|
Registered User
|
|
Join Date: Jul 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

July 5th, 2008, 04:04 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
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.
|
|
 |