Accessing label value using javascript and then manipulating that value

Hi,

      I am having problem as follows.

      I am using Lable variable (asp:label) in a page which is having a master page.Label looks like as follows

<asp:Label ID="mylabel" runat="server"></asp:Label>

..... 

         In the code behind file I am assinging some value to the label as follows

      page_load()

{

mylabel.text=100.ToString();//this value comes at runtime

}

Now I want to access this value of label "mylabel" in the javascript and then i want to add some value (will be some interger of double value) and again display that value to the "mylabel".Please help me in how to do this?.suppose i want to get this functionality agains a button click event on client side.Waiting for the help................

Thanks,

nmeighty.

0
nmeighty
11/19/2007 7:09:45 PM
asp.net.navigation-controls 13714 articles. 0 followers. Follow

17 Replies
2522 Views

Similar Articles

[PageSpeed] 41
Get it on Google Play
Get it on Apple App Store

<button onclick="functioname()" .... />

<script type="text/javascript">
function functionname()
{

var label =  document.getElementById("mylabel");

label.text = ....;

}
</script>

 I think this will work, i m not sure if it is label.text or label.value to assign the value,

Hope this helps


Microsoft geek forever

-> Currently developing my blog...
0
ysoldati
11/19/2007 7:32:29 PM

As far as getting the value of your label into javascript, ysoldati is right, you can just use:

var labelValue = document.getElementById('mylabel.ClientID').value;

But if you want to send that value back to that server side control, you're going to have to save it to some control (the label may work, I'm not sure, if not you can use a hidden field) and then trigger a post back.  During the post back you can read the value of the label/hidden field and then update the label so the user can see the new value.

Hope that wasn't confusing, good luck. 

0
Aeson07
11/19/2007 8:10:03 PM

Hi,

     Thanks you very much for the reply.....and sorry for the late response...actually i was trying different things....Ahhh I forgot onething to mention that I want to get value from a label which is hidden i mean like below

<asp:Label ID="totalcharge" runat="server"></asp:Label> (we will assgn value to this label after some addition through javascript code) 

<asp:Label ID="mytotalhidden" runat="server" Visible="false"></asp:Label>(we will get value from this label to add something)

Now the problem is like when I use to access value from the mytotalhidden then i get java script error like 'object expected'.I am using following code to assign values to  the mytoalhidden and then i use to display them in the alert.here is the code

page_load()

{ 

this.totalcharge.Text = grandtotal.ToString("0.00");//comes at runtime can be some positive decimal value except null or 0

mytotalhidden.Text = grandtotal.ToString("0.00"); //comes at runtime can be some positive decimal value except null or 0

}

then in javascript code i am proceeding as follows

var txt = document.getElementById('<%=mytotalhidden.ClientID%>').innerHTML;

alert('value: '+txt);//to check what i am getting which is alway null

var temp=parseInt(txt);

txt=calculateTax(temp); //method to add some integer like 10 etc

alert(
'value: '+txt);

document.getElementById('<%=totalcharge.ClientID%>').innerHTML = txt;

 waiting for reply

Thanks,

nmeighty.

0
nmeighty
11/20/2007 1:05:05 PM

When you declare something as Visible="false" then there is no client-side HTML generated for that object, so your javascript will never be able to see it.  If you want a hidden field to store information in, then you should use a...well, a hidden field haha.  It looks like:

<input type="hidden" id="mytotalhidden" runat="server" />

Then you can access it in javascript using    document.getElementById('<%=mytotalhidden.ClientID%>').value; (you can access it in code-behind by using .value as well)

Also, as a side note, I don't think you need the "this" keyword in order to use your totalcharge control in page_load.

Hope this helps. 

0
Aeson07
11/20/2007 1:18:47 PM

Hi ,

         Thanks you so much for the reply.Yes I am able to achive the Functionality.I am marking your Reply as answer.........Please can any one guide me of the problem as below.

         In the same page I am also using a table to display some prices.I use to generate this table dynamically in the code behind of the aspx page.Here is the code which server sends to the client side

   <table width="95%" cellpadding="3" border="0" align="center">

        <tr>
       
            <td valign="top" class="TableBorder1">
           
                 <table id="ctl00_contentMain_priceTable" class="Table" border="0" style="width:100%;">
 <tr id="ctl00_contentMain_TableHeaderRow13">
  <th id="ctl00_contentMain_TableHeaderCell1" class="HeaderStyle33" colspan="6">Price Details</th>
 </tr><tr id="ctl00_contentMain_TableRow1">
  <td id="ctl00_contentMain_TableCell1"><span id="ctl00_contentMain_Labelins"><table id="mytable"  width="500px" bordercolor="black" ><tr><td><b>Description</b></td><td><b>Quantity</b></td><td><b>Unit Price</b></td><td><b>Extended Price</b></td></tr><tr><td width="200px">Adult Price</td><td>1</td><td>199.00</td><td><strong>&pound;199</strong></td></tr></br><tr><td width="200px">Price Adjustment</td><td>1</td><td>-35.00</td><td><strong>&pound;-35</strong></td></tr></br><tr><td width="200px">Other Charges</td><td></td><td></td><td><strong>&pound;86.90</strong></td></tr><tr><td></td><td></td><td></td><td><hr/></td></tr><tr><td width="200px">Total</td><td></td><td></td><td><strong>&pound;250.90</strong></td></tr></table></span></td>
 </tr>
</table>

you can see 250.90 in the third last line.this price also comes at runtime(Now the problem is how can i use some variable or something else to reference this value becuse it can be 344 ,300 etc) .Now I want to change it to the same value i will calculate using javascript value.i-e

In java script I will get the value from hidden input and then will proceed as follows

var txt = document.getElementById('<%=mytotalhidden.ClientID%>').innerHTML;

alert('value: '+txt);//to check what i am getting which is alway null

var temp=parseInt(txt);

txt=calculateTax(temp); //method to add some integer like 10 etc

alert(
'value: '+txt);

document.getElementById('<%=totalcharge.ClientID%>').innerHTML = txt;

now i want to get the position of 250.90 and replace with the value of txt variable .

Please help .....

Thanks,

nmeighty

 

 

 

 

0
nmeighty
11/21/2007 11:47:11 AM

I'm not sure if this is the best solution or not, but one idea that comes to mind is to store the price value that you want to be able to update in some variable or control, and then add that value into your table using <%=whatever%>

That way you could use javascript to update "whatever" to anything you need, and it will be placed back into the table on a post back. 

0
Aeson07
11/21/2007 12:09:30 PM

Hi,

    Thanks again.Is there any way to change the price of this span using javascript? you can see that the price is currently 250.90.I want to access this and then change the value after calculating ... 

<span id="ctl00_contentMain_Search_lblSearch"><br><b>Date: </b> 03 January, 2008<br><b>Total Price: </b> &pound;250.90<br></span>

Thanks,

nmeighty.

0
nmeighty
11/21/2007 12:32:47 PM

 put the price in a label. give it an id and you should be able to acces it from js.

something like 

<span id="ctl00_contentMain_Search_lblSearch">

    <br>

     <b>Date: </b> 03 January, 2008

     <br>

     <b>Total Price: </b> &pound;

    <label id="myLabel" value="250.90"/><br>

</span>


i think you example is too complicated 

0
iliemail
11/21/2007 1:31:48 PM

Hi,

      Yes you are very much right in saying the example is too much complex.Now I will try to make the scenerio more clear.I needed to generate a table at runtime in the code behind of some file say "myfile.aspx".Now i use to generate the table as follows

I used a label with ID "lab" and then in that label i used to do as follows

lab.Text="<table id=\"mytable\" width=\"500px\" bordercolor=\"black\" ><tr><td><b>Description</b></td><td><b>Quantity</b></td><td><b>Unit Price</b></td><td><b>Extended Price</b></td></tr>";

foreach (Pricedetail d in detaillist.price) //detailist is a list of Pricedetail type objects ,it can contan any number of prices .

{

grandtotal += d.ExtendedCost;

othercharges += d.ExtendedCost;

grandtotal += d.ExtendedCost;

lab.Text += "<tr>";

lab.Text += "<td width=\"200px\">" + CommonFunctions.ToTitleCase(d.Description) + "</td><td>" + d.Quantity + "</td><td>" + d.UnitCost.ToString("0.00") + "</td>" + "<td><strong>&pound;" + d.ExtendedCost;

lab.Text += "</strong>" + "</td></tr></br>";

grandtotal += d.ExtendedCost;

}

}

othercharges += bookingCharges;

if (othercharges>0)

{

lab.Text = lab.Text +
"<tr>";

lab.Text += "<td width=\"200px\">" + "Other Charges" + "</td><td></td><td></td>" + "<td><strong>&pound;" + othercharges.ToString("0.00");

lab.Text += "</strong></td></tr>";

}

grandtotal += 10;

lab.Text +=
"<tr><td></td><td></td><td></td><td><hr/></td></tr>";

lab.Text = lab.Text + "<tr>";

lab.Text += "<td width=\"200px\">" + "Total" + "</td><td></td><td></td>" + "<td><strong>&pound;" + grandtotal.ToString("0.00");//this is the line where i need to insert label .Now how can i do that?? lab.Text += "</strong></td></tr>";

lab.Text += "</table>";

Now this is the line where i need to insert label .Now how can i do that??third last line of the code

lab.Text += "<td width=\"200px\">" + "Total" + "</td><td></td><td></td>" + "<td><strong>&pound;" + grandtotal.ToString("0.00");//

Thanks,

nmeighty.

0
nmeighty
11/21/2007 3:09:20 PM

To insert your label/field/whatever into your line you would just use:

lab.Text += "<td width=\"200px\">" + "Total" + "</td><td></td><td></td>" + "<td><strong>&pound;" + myhiddentotal.value;

or replace myhiddentotal.value with whatever control you may be using (myTotalLabel.Text, etc)

Hope this helps. 

0
Aeson07
11/21/2007 3:21:50 PM

Hi,

      It will not solve the problem,because it will only insert the value at the position below

lab.Text += "<td width=\"200px\">" + "Total" + "</td><td></td><td></td>" + "<td><strong>&pound;" + myhiddentotal.value;

    .However I want to change that value using javascript .However i have tried to use as below

lab.Text += "<td width=\"200px\">" + "Total" + "</td><td></td><td></td>" + "<td><strong>&pound;<asp:Label runat=\"server\" Value="+grandtotal.Tostring("0.00")+" ID=\"Totalamount\" ></asp:Label>";

but its also not working..

Thanks,

nmeighty.

0
nmeighty
11/21/2007 5:51:30 PM

All of this code is executed in the code-behind (I.E. during a postback) the only time lab.Text is going to be updated with your new values is when you post back to the server via something like a button click.  Theres a way to call a post back from javascript too, but I'm not very familiar with the code, just do a search for __dopostback or javascript post back and you should find plenty of threads on the forum about it.

0
Aeson07
11/21/2007 6:27:14 PM

Hi,

     Yes I think I need to learn some more javascript and also will need to change the coding.Thanks you very much for all the help you have done so far.Thanks to all

Thanks,

nmeighty.

0
nmeighty
11/21/2007 7:09:07 PM

 

Why is important to use javascript? can'y you use a grid view?
0
iliemail
11/21/2007 7:52:11 PM

Hi,

     I have to use javascript its the requirement...no worries..

Thanks,

nmeighty

0
nmeighty
11/22/2007 11:29:02 AM

Hi,

     I have to use javascript its the requirement...no worries..and i cant use gridview in the current scenerio.I have used some label and span variables and hopefully i will be able to get the desired results.

Thanks,

nmeighty

0
nmeighty
11/22/2007 11:29:58 AM

Hi,

    Okay lets hava a scenerio that we hava list of prices at runtime from database.there can be any number of prices may be 2,3,4,5,and so on...> Now we we need to display them at run time in a good format.Now I got the logic to generate a table at runtime in the codebehind file of mypage.aspx.So i did the above thing.Also the same total is represendted at 3 different locations of the page. One in the dynamically generated table,second in some usercontrol and the third one is a simple label.Now I have a button.When user clicks on that button then i need to change values at all the 3 locations.....

Thanks,

nmeighty.

0
nmeighty
11/22/2007 12:11:13 PM
Reply:

Similar Artilces:

superreview requested: [Bug 301391] Include some child values in concatenated accessible names : [Attachment 189854] Other than for items whose values are a URL, use accessible values when concatenin
Aaron Leventhal <aaronleventhal@moonset.net> has asked Boris Zbarsky <bzbarsky@mit.edu> for superreview: Bug 301391: Include some child values in concatenated accessible names https://bugzilla.mozilla.org/show_bug.cgi?id=301391 Attachment 189854: Other than for items whose values are a URL, use accessible values when concatening an accessible name https://bugzilla.mozilla.org/attachment.cgi?id=189854&action=edit ...

superreview granted: [Bug 301391] Include some child values in concatenated accessible names : [Attachment 189854] Other than for items whose values are a URL, use accessible values when concatening
Boris Zbarsky <bzbarsky@mit.edu> has granted Aaron Leventhal <aaronleventhal@moonset.net>'s request for superreview: Bug 301391: Include some child values in concatenated accessible names https://bugzilla.mozilla.org/show_bug.cgi?id=301391 Attachment 189854: Other than for items whose values are a URL, use accessible values when concatening an accessible name https://bugzilla.mozilla.org/attachment.cgi?id=189854&action=edit ...

new to .net, need help setting db values from checkbox's then retrieving value to a label control
Hi, Being new to asp.net i know this possible i just can't quite get it right!  I need a page with 5 checkbox's that when submitted sets the bit values in DB fields to checked. Then i need another page that retrieves the records of the checkboxes, but instead of having the db pre-set checkboxes on the second page i want it to turn on label web controls depending on if the check box is set.. I hope that makes sense?!! Can anyone point me in the right direction or to a suitable tutorial? Thanks in advance Ben Excellent article here.http://www.heliosfx.com...

using stored values as control values
I am trying to find out if it is possible to store the name of a menu item as a variable and then use the stored value. example string ls_menu_item ls_menu_item = 'report' m_menu.ls_menu_item.visible=TRUE in stead of coding m_menu.report.visible=TRUE. I have approximately 28 menu items that need to be enabled dynamically I am using PB 7.0.3 for this. Thanks Take a look at PFC's pfc_n_cst_menu for examples... -- This is a FAQ, read Help, then search www.groups.google.com/advanced_group_search pbm_thisusuallydoesnothelp:-)) Philip Sal...

Control.Value and Control.SelectedItem.Value
Can someone explain to me the difference between say, Dropdownlist.SelectedItem.Value and Dropdownlist.SelectedValue?? Just curious, Thanks!Tim These are going to return the same thing. dropdownlist.selectedvalue is a shortcut to get the currently selected value in the dropdown. If that is all you want then you can just use that. If you want to access the currently selected item you can use dropdownlist.selecteditem and that return the item and you can then access any of the item's properties, including in this case the value property. If this post answered your question please r...

Getting value from a textbox control that was filled with a value from javascript
 I have a textbox control (seen below txtMileageDollars1) that gets filled with javascript based on the number of miles a user types into another textbox (txtMileageMiles1) control.  When I post back with the submit button, the value is blank.  I've tried setting EnableViewState to false, but still cannot get a value on postback.  Does anyone have a solution?   Miles<asp:TextBox ID="txtMileageMiles1" runat="server" Width="50px"></asp:TextBox>   Total (txtMileageMiles1 * rate with javascript)<asp:TextBox...

Data grid value used to set label value
I have a datagrid with a 'gender' field. In the database, it is either M or F. Only one record is returned Here's what I want to do. 1. Display in the datagrid Male if the database value is M and Female is the value is F. If that can't be done then here's option 2: 2. Change the text value of a label to Male if the database value is M ... I'm not sure how to look at a particular value in the datagrid. Very simple... Simply write a function that you call after binding the data to the grid, then loop through all datagrid items, pull the label value out, and change it ...

Is it possible to use datetime values as control value in loop?
My users are going to select a check in date and a check out date. What I need to do is loop through all dates in between the selected dates and check what the price of accommodation will be for each date. I can't seem to get the loop to increment the day value - not sure if there is another better way to do this, or if I'm missing something in syntax. Your help is appreciated! for (DateTime dayOfStay=checkInDate; dayOfStay <= checkOutDate; dayOfStay.AddDays(1)) { foreach(DataRow dr in dsSeasons.Tables[0].Rows) { if((dayOfSta...

Fill a label Text value with the return value of a javascript
I wonder if it is possible to do something like that:   <script type="text/javascript">function puttext() {return "this is the text";}</script> <asp:Label ID="Label1" runat="server" Text="<%javascript:puttext(); %>"></asp:Label>   Am I just writing it with the wrong syntax or it is simply not possible? Try it like this:  <script type="text/javascript">function puttext(contorl) {control.innerHTML = "this is the text";}</script> <asp:Label ID="Label1...

TextBox values check with dropdownlist values using javascript
 Hi,I have a Textbox and dropDownlist.I am showing database values into the dropdown list.When user enters something into database. And if entered value is existing in the dropdownlist alert should give as 'This value already exists.'This i want to do using javascript.I need it very very badly.Please reply.thanks   for(var vLoop=0; vLoop<document.getElementById("<% dropdownlst1.ClientId %>").options.length  ; vLoop++){         if(document.getElementById("<% dropdownlst1.ClientId %>").opt...

how to accessing values and set values for a controls that is inside a datagrid
hi this is chandra mohan working as software engineer . here i am having problem with accessing valuse and setting values for controls inside a datagrid. actually i am having datagrid and in this grid i am having item templates of one dropdwon list and text box. i having one Reset link buttion out side the grid(that is in the form) by clicking this Reset Link buttion it should invokes the javascript function and in that function it should set all textboxes of datagrid to  .. txtActionVal.readOnly = true;   txtActionVal.Value ="";and dropdownlist to  dropdownlist...

using javascript getting value from textbox when edit button is clicked and both controls are present in gridview control
Hi All using javascript I want to get value  from textbox when edit button is clicked and both controls that is texbox and button are present in gridview control .Alok Hi aloksinha83, Please see this post: http://forums.asp.net/t/1069245.aspx Thanks,Qin Dian TangMicrosoft Online Community SupportPlease remember to mark the replies as answers if they help and unmark them if they provide no help....

how to get label control value in javascript.
 Hi I need to get the label Text when the User clicks the browser close button.function fnPassValues() {     if((window.event.clientX < 0) || (window.event.clientY < 0))     {        var temp = document.getElementById("<%=lblValue.ClientID%>").innerText;        alert(temp);     } }If i set the value like this, its working correctly. <asp:Label ID="lblValue" Text="3" Height="20px" runat="server" CssClass=&quo...

If Not value or value then....
Hi all,I want to write a simple IF Not then statement in ASP.net 2.0 and I would like some help please.  If Not ntuser = "DOMAIN\ntuser" Or ntuser = "DOMAIN\ntuser2" Then Panel1.Visible = False End If Doesnt work, can you tell me where i'm going wrong please?many thanks.  The first place you're going wrong is in telling us it "doesn't work" without providing any details. JeffPlease: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue. Use Not as stated below. It ...

Web resources about - Accessing label value using javascript and then manipulating that value - asp.net.navigation-controls

123D Tutorial: Manipulating components and incorporating design intent - YouTube
http://www.123Dapp.com/support - get the steps and model and do this tutorial yourself! Have few extra parts left over after putting your project ...

Doctor Mervyn Jacobson jailed for a year for manipulating the sharemarket
A doctor who once claimed to be worth $100 million has been jailed for a year for manipulating the sharemarket for financial gain.


Doctor Mervyn Jacobson faces lengthy jail sentence after being found guilty of manipulating stock market ...
He has been praised by American presidents and British lords for his wildlife conservation efforts, and claims to have saved the brush-tailed ...

Facebook's Adam Kramer defends controversial emotion manipulating study
The creator of the controversial Facebook study now admits the findings "may not have justified all of this anxiety".

Australia accused of manipulating Australian dollar to save iron ore miners
... iron ore miners are so desperate for cash that ''even the kangaroos are for sale". An American iron ore miner says Australia is "manipulating" ...

Government accused of manipulating science news
The federal government engages in "unacceptable political interference" in the communication of government science, says the head of a group ...

Commentary: Japan's Abe manipulating a dangerous coup against pacifist Constitution
TOKYO,June30JapanesePrimeMinisterShinzoAbeismanipulatingadangerouscouptooverturnthecountry'spost-w

Over 50 US military analysts say Pentagon is manipulating Daesh reports
Over 50 US military analysts say Pentagon is manipulating Daesh reports

Volkswagen ordered to recall nearly 500,000 vehicles for manipulating software to circumvent emissions ...
Volkswagen has been manufacturing cars that only fully deploy their emissions control systems when they are undergoing fuel emissions inspection, ...

Resources last updated: 11/24/2015 5:49:44 PM