f



Getting value from asp:label into javascript input value

Hello,
I really hope that someone can help me resolve my problem, I've been
working on it for the past few days, to no avail.

I have an asp:label which gets its value from the database, but what
I'm trying to do is to get that value (from the asp:label) and place it
inside the javascript input value.

I can post sample code if required.

TIA

D

0
Dwizz20 (1)
8/5/2005 10:07:24 AM
comp.lang.javascript 38370 articles. 2 followers. javascript4 (1315) is leader. Post Follow

10 Replies
968 Views

Similar Articles

[PageSpeed] 40

"Dwizz" <Dwizz20@yahoo.co.uk> wrote in message 
news:1123236443.969946.4850@g47g2000cwa.googlegroups.com...
> Hello,
> I really hope that someone can help me resolve my problem, I've been
> working on it for the past few days, to no avail.
>
> I have an asp:label which gets its value from the database, but what
> I'm trying to do is to get that value (from the asp:label) and place it
> inside the javascript input value.
>

give the <label> an id, and use getElementById(...)

....
var lbl = document.getElementById("labelId");
alert(lbl.value);
....

-- 
Dag.


0
Dag
8/5/2005 10:53:31 AM
Thanks for the quick responce Dag,

so just to clarify,

My ASP:Label is as follows
<asp:Label id="Label2" font-names="Arial" font-size="X-Small"
runat="server">Label</asp:Label>

my Javascript input should be as follows;
<script>
var lbl = document.getElementById(Label2);
alert(lbl.value);
</script>

<input id="Label2" readonly="readonly" type="text" value=""
name="unitprice[]" />

0
Dwizz
8/5/2005 12:37:35 PM
Dwizz wrote:
> Thanks for the quick responce Dag,
> 
> so just to clarify,
> 
> My ASP:Label is as follows
> <asp:Label id="Label2" font-names="Arial" font-size="X-Small"
> runat="server">Label</asp:Label>
> 
> my Javascript input should be as follows;
> <script>
> var lbl = document.getElementById(Label2);
> alert(lbl.value);
> </script>
> 
> <input id="Label2" readonly="readonly" type="text" value=""
> name="unitprice[]" />
> 

I know aything about asp, but something like that ?

<input id="Label2" readonly="readonly" type="text"
  name="unitprice[]"
  value="<% execute "response.write Label" %>" />

where *Label* would be your asp Label value

-- 
Stephane Moriaux et son [moins] vieux Mac
0
ASM
8/5/2005 1:51:40 PM
Thanks for the reply ASM,
but when I use the code below I get a compiler error saying execute is
not declared

> <input id="Label2" readonly="readonly" type="text"
>   name="unitprice[]"
>   value="<% execute "response.write Label" %>" />
> 
> where *Label* would be your asp Label value

0
Dwizz
8/5/2005 2:09:45 PM
Thanks for the reply ASM,
but when I use the following code I get a compiler error, saying
execute is not declared


<input id="Label2" readonly="readonly" type="text"
  name="unitprice[]"
  value="<% execute "response.write Label" %>" />

0
Dwizz
8/5/2005 2:12:20 PM
Dwizz wrote:
> I get a compiler error, saying
> execute is not declared

As said : I know anything about asp
And you ?

Found this code in a site given by Google

>   value="<% execute "response.write Label" %>" />

execute :
http://support.microsoft.com/kb/q224363/

I said *something like that*
In other words,
You have via asp :
- to declare a variable i.e : 'Label'
- and to give to it the value you want to display
then (I hope this time it will be corect ?) :

<input id="Label2" readonly="readonly" type="text"
   name="unitprice[]"
   value="<% =Label" %>" />

-- 
Stephane Moriaux et son [moins] vieux Mac
0
ASM
8/5/2005 3:49:06 PM
"Dwizz" <Dwizz20@yahoo.co.uk> wrote in message 
news:1123245455.237131.80770@f14g2000cwb.googlegroups.com...
> Thanks for the quick responce Dag,
>
> so just to clarify,
>
> My ASP:Label is as follows
> <asp:Label id="Label2" font-names="Arial" font-size="X-Small"
> runat="server">Label</asp:Label>
>
> my Javascript input should be as follows;
> <script>
> var lbl = document.getElementById(Label2);
> alert(lbl.value);
> </script>
>
> <input id="Label2" readonly="readonly" type="text" value=""
> name="unitprice[]" />

Almost... You must quote the id:
var lbl = document.getElementById("Label2");

But maybe I'm wrong here... asp:xxx is dotnet, isn't it?

I thought you asked how to get the content of a html <label> tag.
In the sample above, you're refering to <asp:label>, and I'll have to admit
I don't have a clue what that is.

Anothewr thing is that an id attribute must be unique within the page
(You repeat it in the <input> tag...

-- 
Dag.



0
Dag
8/5/2005 7:16:53 PM
Dwizz said the following on 8/5/2005 6:07 AM:

> Hello,
> I really hope that someone can help me resolve my problem, I've been
> working on it for the past few days, to no avail.
> 
> I have an asp:label which gets its value from the database, but what
> I'm trying to do is to get that value (from the asp:label) and place it
> inside the javascript input value.

Then have the ASP put it into a javascript variable.

-- 
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
0
Randy
8/5/2005 11:22:51 PM
Dwizz wrote:
> Thanks for the quick responce Dag,
> 
> so just to clarify,
> 
> My ASP:Label is as follows
> <asp:Label id="Label2" font-names="Arial" font-size="X-Small"
> runat="server">Label</asp:Label>
> 
> my Javascript input should be as follows;
> <script>

The type attribute is required (but is not likely to be the source of 
your problem):

   <script type="text/javascript">

> var lbl = document.getElementById(Label2);

Add quotes around the id as per Dag's advice:

   var lbl = document.getElementById("Label2");

> alert(lbl.value);
> </script>
> 
> <input id="Label2" readonly="readonly" type="text" value=""
> name="unitprice[]" />
> 

A third issue is that your script is before the element it attempts to 
access.  because your attempt to set 'lb' is outside any function, it 
is run as the page loads.  Since the element with id 'Label12' hasn't 
been reached yet, it doesn't exist.  So even with other errors fixed, 
it'll still fail.

Put the script after the element or run it onload.

-- 
Rob
0
RobG
8/6/2005 5:45:39 AM
In regular asp the syntax for assigning an asp global variable to a
javascript var is:

var jscriptVar = <%=aspVar%>

I don't know if this carried over to ASP.NET.

0
bgulian
8/7/2005 2:28:23 AM
Reply: