467,915 Members | 1,206 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,915 developers. It's quick & easy.

Combine Javascript and ASP.Net

Hello Everyone,

I am ready to pull my hair out with this situation.
Here is what my application Looks like.

I have a Master Page,Content Page with a Tab Container and A user Control inside the Tab Panel of the Container.

In the user Cotrol I have a Table with Textboxes. My assignment is to Calculate the average of two textboxes and diplay in a Label. I need to do that on the client so that there is no postback so I try to use javascript.

A declared a Javascript Function and try to call it OnBlur of the textbox. How do I pass the values of txt1 and txt2 to the function?
Jan 11 '10 #1
  • viewed: 4111
Share:
30 Replies
ThatThatGuy
Expert 256MB
Define the function as
Expand|Select|Wrap|Line Numbers
  1. calculate(val1,val2)
  2. {
  3. //code for average
  4. }
  5.  
call the function on button click event and pass the parameters
Jan 12 '10 #2
make a func to do calculation in java script then use it in events of textbox u use in your tab panel
Jan 12 '10 #3
Frinavale
Expert Mod 8TB
Check out this article on How to use JavaScript in ASP.NET.

Please note that you have to use the ClientID of the TextBoxes in order to retrieve the TextBox elements in your JavaScript Code since you are using a MasterPage. The ClientID property is there to give you access to the ID that the element is given when it is rendered as HTML...

Say you have a TextBox with the ID="TextBox1". When TextBox1 is rendered it is given a unique id. Since you can have multiple controls or pages with a TextBox with the ID="TextBox1", ASP.NET takes it onto itself to ensure that these TextBoxes are given unique IDs when they are rendered as HTML. This means that the ID given to TextBox1 in your user control in your MasterPage might look this in it HTML:

Expand|Select|Wrap|Line Numbers
  1. <input type="text" id="ctl00_IdOfPlaceHolder_IdOfUserControl_TextBox1" ..... />
See how different this ID is from the ID you gave the TextBox in your server code? The way to access this ID is through the TextBox's ClientID property.

Here is an example of JavaScript code that is not dynamically generated (that is just part of the page for the user control):
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.   function avg()
  3.   {
  4.     //retrieving the value from the TextBox1 element.
  5.     var firstValue = document.getElementById('<%=TextBox1.ClientID;%>').value;
  6.     //retrieving the value from the TextBox2 element.
  7.     var secondValue = document.getElementById('<%=TextBox2.ClientID;%>').value;
  8.  
  9.      //now use these values to calculate the average...
  10.   }
  11. </script>
In this JavaScript code I am using the TextBox's ClientIDs to retrieve the text box elements. Notice how I used the <%= %> syntax. This is ASP shorthand for calling the Response.Write() method. This will write the ClientID into the place where it is needed. Anything within the <% %> tags is executed server side. That means that this has to be either VB or C# code (whichever you're using for your server side code). In this example I used C# code. If I were to have used VB.NET code it would have been very similar except that there wouldn't be any semicolon at the end of the ClientID.

(Please do not use Response.Write in your C#/VB.Net code because it will write whatever it is you're writing above the <html> start tag).

-Frinny
Jan 12 '10 #4
Thanks every one for the code and ideas. Here is the situation. Lest say I have 5 rows with 3 columns. In each row I have 2 Textboxes and 1 Label. What I need to do is have the Javascript calculate onBlur of the text box.I need to find the textbox that caused the event and the one next to it in the same row that will be used to calculate average. and display it in the label that is in the same row. Do i need a javascript function for Each row?? Also where do I place the Javascript, into the master page , Content Page or User Control.
Jan 12 '10 #5
Frinavale
Expert Mod 8TB
No you don't need a JS function for each row.
You can pass the object that caused the event into the function. You can access it using the "this" keyword.

For example here's the average function that takes a TextBox element as a parameter:
Expand|Select|Wrap|Line Numbers
  1. function avg(tbElement)
  2. {
  3.  
  4. }
Here's the code that passes the TextBox element into the avg function:
Expand|Select|Wrap|Line Numbers
  1. onblur="avg(this)"
  2.  

It would probably be a good idea to keep track of the grid that contains the TextBoxes in your average function so that you can retrieve all of the rows...loop through the rows and do your average calculation.

Expand|Select|Wrap|Line Numbers
  1. function avg(tbElement)
  2. {
  3.     var grid = document.getElementById('theIdOfTheGrid');
  4. }

You can use the getElementsByTagName() method to retrieve the rows in the grid ("tr" is the tag name). You can likewise use this method to retrieve all of the text box elements for each row in the grid (tag name is "input" you can check the type property of each input element you retrieve to make sure it's type "text").

You can put the JavaScript at any level you want to.
I would keep it at the UserControl level though because it's likely that this method wont have to be used by anything else. You could store it in an external JavaScript file if you want to...and just link to it when the UserControl is active...meh.

Why didn't you just ask your real question first of all??

-Frinny
Jan 12 '10 #6
ok so I would need to Give the row of the table a name, how would I pass the row onBlur of the textbox and loop through the row and find the controls. I have tried passing this and get a javascript null object error.
Jan 12 '10 #7
Frinavale
Expert Mod 8TB
I edited my post so please look it over again.
What you'll do is loop through all of the rows and retrieve the inputs for each row...then you loop through each input retrieved and check if it has a "type='text'" and if so check if it's the same element passed into the method. If it is then that's the row where the event happened.

This question is probably best to ask a JavaScript expert because they'd probably know more about this than I do. I've moved your question to the JavaScript Forum.

-Frinny
Jan 12 '10 #8
Do I have to use HTML controls or can I do the same with ASP.Net Textbox,Label?
Thanks
Jan 12 '10 #9
Frinavale
Expert Mod 8TB
When you are using JavaScript you have to work with HTML elements.

Your ASP.NET controls are rendered as HTML controls. You cannot use .NET controls in your JavaScript because they exist on the server and JavaScript works in the browser, client-side.

You can access the HTML elements that represent the ASP.NET controls using JavaScript.

-Frinny
Jan 12 '10 #10
Here is the code for what I am currently doing and i know it is a hack and only works with one row of my table. Maybe we can find a way to modify it and make it work without hardcoding those Control Name's.

Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2.     {
  3.         string MyScript = "function CalcAverage() {" +
  4.         "var nSteps; " +
  5.         "var nDays; " +
  6.         "var nAverage; " +
  7.         "nSteps = 0; " +
  8.         "nDays = 0; " +
  9.         "nAverage = 0; " +
  10.         "nSteps = document.getElementById('ctl00_ctl00  _cphBody_cphBody_txtTotalSteps').value; " +
  11.         "nDays = document.getElementById('ctl00_ctl00_cphBody_cphBody_txtDaysOfSteps').value; " +
  12.  
  13.         "if ((nSteps != 0) && (nDays != 0)) " +
  14.         "{ " +
  15.         "nAverage = (nSteps / nDays); " +
  16.         "nAverage = Math.round(nAverage); " +
  17.         "document.getElementById('ctl00_ctl00_cphBody_cphBody_lblAvgSteps').value = (nAverage); " +
  18.         "} " +
  19.         "else " +
  20.         "{ " +
  21.         "document.getElementById('ctl00_ctl00_cphBody_cphBody_lblAvgSteps').value = 0;}; " +
  22.         "}; ";
  23.  
  24.         ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "MyScript", MyScript, true);
  25.  
  26.         txtTotalSteps.Attributes["onblur"] = "CalcAverage()";
  27.         txtDaysOfSteps.Attributes["onblur"] = "CalcAverage()";
  28.     }
Jan 12 '10 #11
Frinavale
Expert Mod 8TB
Well.......I told you how to do this already.

First of all replace "ctl00_ctl00_cphBody_cphBody_lblAvgSteps" with lblAvgSteps.ClientID.

You are probably going to want to pass the TextBox element that caused the onblur event as a argument to the CalcAverage() method. You have to change the CalcAvarage method to accept the text box as a parameter too....


Expand|Select|Wrap|Line Numbers
  1.   //passing the textbox element as an argument into the CalcAvarage method
  2.   txtTotalSteps.Attributes["onblur"] = "CalcAverage(this)";
  3.   txtDaysOfSteps.Attributes["onblur"] = "CalcAverage(this)";
  4.  

Expand|Select|Wrap|Line Numbers
  1. string MyScript = "function CalcAverage(txtBoxElement) {" +
  2.         "var nSteps; " +
  3.         "var nDays; " +
  4.         "var nAverage; " +
  5.         "nSteps = 0; " +
  6.         "nDays = 0; " +
  7.         "nAverage = 0; " +
  8.         "nSteps = document.getElementById('"+
  9.          txtTotalSteps.ClientID +
  10.          "').value; " +
  11.         "nDays = document.getElementById('"+
  12.          txtDaysOfSteps.ClientID +
  13.          "').value; " +
  14.  
  15.         "if ((nSteps != 0) && (nDays != 0)) " +
  16.         "{ " +
  17.         "nAverage = (nSteps / nDays); " +
  18.         "nAverage = Math.round(nAverage); " +
  19.         "document.getElementById('"+
  20.          lblAvgSteps.ClientID 
  21.          "').value = (nAverage); " +
  22.         "} " +
  23.         "else " +
  24.         "{ " +
  25.         "document.getElementById('" +
  26.          lblAvgSteps.ClientID +
  27.          "').value = 0;}; " +
  28.         "}; ";

I'm assuming lblAvgSteps is a Label.
An ASP.NET Label is rendered as a <span> element, where the text for the Label is placed as the "inner html" of the span element. So it looks like:
Expand|Select|Wrap|Line Numbers
  1. <span> This is the Text Property of the of the label </span>
Therefore, if you want to change the text of an ASP.NET Label client side, you have to use the innerHTML property of the span element that represents the Label.


So you need to change:
Expand|Select|Wrap|Line Numbers
  1. "document.getElementById('"+
  2.          lblAvgSteps.ClientID 
  3.          "').value = (nAverage); "
To:
Expand|Select|Wrap|Line Numbers
  1. "document.getElementById('"+
  2.          lblAvgSteps.ClientID 
  3.          "').innerHTML= (nAverage); " 
(PS I may have the case wrong for the innerHTML property...double check this or it won't work)

-Frinny
Jan 12 '10 #12
Frinavale
Expert Mod 8TB
You can use the "parentNode" of the HTML text box element to get the parent element of the TextBox...which will give you the Row that it belongs to (make sure to check that the parent element is what you're looking for before you use it).

-Frinny
Jan 12 '10 #13
Lets see if we can find a Javascript guru that can help us. Since i cant debug javascript I dont know what is causing the errors.
Jan 12 '10 #14
I am getting a Javascript Object Required Error. Looks like the Javascript does not recognize the Textboxes.
Jan 12 '10 #15
Frinavale
Expert Mod 8TB
If you use IE8 it comes with a debugger tool.
Hit F12.

If you use FireFox then download something like FireBug (this is what I use).


-Frinny
Jan 12 '10 #16
Just finished the function that Recognizes server controls. So this would work if I had only one set of textboxes to calculate the average.

Now I need to figure out how to pass a row to Javascript, loop throgh the rows and find the controls then calculate the average. phew...

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript">
  2.     function CalcAverage() {
  3.         var txtSteps = document.getElementById('<%= txt1.ClientID %>');
  4.         var txtDays = document.getElementById('<%= txt2.ClientID %>');
  5.         var txtAverage = document.getElementById('<%= txt3.ClientID %>');
  6.         var nSteps = 0;
  7.         var nDays = 0;
  8.         var nAverage = 0;
  9.         if (txtSteps.value != '')
  10.             nSteps = parseInt(txtSteps.value);
  11.         if (txtDays.value != '')
  12.             nDays = parseInt(txtDays.value);
  13.         if ((nSteps != 0) && (nDays != 0)) {
  14.             nAverage = (nSteps / nDays);
  15.             nAverage = Math.round(nAverage);
  16.             txtAverage.value = nAverage;
  17.         }
  18.         else {
  19.             txtAverage.value = 0;
  20.         }
  21.  
  22.     }        
  23.  
  24.     }  
  25. </script>
C# Code Behind

Expand|Select|Wrap|Line Numbers
  1.     protected void Page_Load(object sender, EventArgs e)
  2.     {
  3.         txt1.Attributes["OnBlur"] = "CalcAverage()";
  4.         txt2.Attributes["OnBlur"] = "CalcAverage()";
  5.     }
Jan 12 '10 #17
Hello,

I Am new to javascript. I am trying to create a function like this...
My main problem are these lines for code

var variable = "TotalSteps";
var element = "'" + '<' + '%' + '= ' + 'txt' + variable + '.ClientID' + ' %' + '>' + "'"
var txtSteps = document.getElementById(element);

here is the complete Function.

Expand|Select|Wrap|Line Numbers
  1. function CalcAverage() {
  2.         var variable = "TotalSteps";
  3.         var element = "'" + '<' + '%' + '= ' + 'txt' + variable + '.ClientID' + ' %' + '>' + "'"
  4.         var txtSteps = document.getElementById(element);
  5.         var txtDays = document.getElementById('<%= txtDaysOfSteps.ClientID %>');
  6.         var txtAverage = document.getElementById('<%= txtAvgSteps.ClientID %>');
  7.         var nSteps = 0;
  8.         var nDays = 0;
  9.         var nAverage = 0;
  10.         if (txtSteps.value != '')
  11.             nSteps = parseInt(txtSteps.value);
  12.         if (txtDays.value != '')
  13.             nDays = parseInt(txtDays.value);
  14.         if ((nSteps != 0) && (nDays != 0)) {
  15.             nAverage = (nSteps / nDays);
  16.             nAverage = Math.round(nAverage);
  17.             txtAverage.value = nAverage;
  18.         }
  19.         else {
  20.             txtAverage.value = 0;
  21.         }
  22.     }
Jan 13 '10 #18
Dormilich
Expert Mod 8TB
this stuff (<%= Ö %>) looks like ASP code. before JavaScript executes, all ASP is processed, thus calling <%= Ö %> in JavaScript does nothing.
Jan 13 '10 #19
What i am doing is tring to find server side controls. I do not want to hard code the Control names in the function but pass them as parameters and build a Var that will represent '<%= txtControlName.ClientID %>' That way i have a flexible function. All i do is pass the control Name and calculate my averages.
Jan 13 '10 #20
Dormilich
Expert Mod 8TB
as I said, ASP is done server side, once youíre at the client, ASP tags are mere text.
Jan 13 '10 #21
Maybe we are not understanding each other. Do you agree that server control attributes and properties can me manipulated within a Javascript function? Also by using document.getElementById('<%= ServerControlID.ClientID %>');
you can access those controls?
Jan 13 '10 #22
Dormilich
Expert Mod 8TB
Iím in no way an ASP expert, all I know is, that any ASP controls created by JavaScript are not executed by ASP. you just canít do server side code on the client. see also the source code of your document in the browser.
Jan 14 '10 #23
Thanks for the reply. I am not expecting you to know ASP.net since I am posting this a javascript question. If anyone that reads this knows ASP.Net and javascript and has run Into this situation please answer.
Jan 14 '10 #24
Frinavale
Expert Mod 8TB
Stefbek97, please do not double post your question. It divides our attempts to help you, thus making it hard for us to see what other experts have already advised and it makes it difficult for you to get an answer to your question.

I've merged your double posts together.
Jan 14 '10 #25
Frinavale
Expert Mod 8TB
In post #4 I posted the following:
...Notice how I used the <%= %> syntax. This is ASP shorthand for calling the Response.Write() method. This will write the ClientID into the place where it is needed. Anything within the <% %> tags is executed server side. That means that this has to be either VB or C# code (whichever you're using for your server side code which will send the page to the server and then send a new page back to the client)...
Did you not understand what I was trying to say here?

Do you agree that server control attributes and properties can me manipulated within a Javascript function? Also by using document.getElementById('<%= ServerControlID.ClientID %>');
you can access those controls?
You cannot manipulate Server Control attributes and properties with JavaScript.
I said this already (see post #10). Dormilich is right, once the server code is finished it generates text (HTML is just text) that is sent to the browser. The browser then reads the HTML-text that was generated by the server and displays things accordingly. You cannot access server-side controls once the page has been sent to the browser because all server-side objects are destroyed once the page is sent.

JavaScript works in the browser. It cannot access anything on the server for 2 reasons...there is no persistent connection open between the browser and the server...and the server-side controls do not exist after the page has been sent to the browser.


My main problem are these lines for code....
In the following code:
Expand|Select|Wrap|Line Numbers
  1. var variable = "TotalSteps";
  2. var element = "'" + '<' + '%' + '= ' + 'txt' + variable + '.ClientID' + ' %' + '>' + "'"
You are declaring a JavaScript variable that contains a String that contains "<%=txtTotalSteps.ClientID %>"

I think you were expecting this to call the server-side code to get the ClientID of the "txtTotalSteps" TextBox...But this is not going to accomplish anything because this is just a String stored into a variable in your JavaScript code. You cannot call server code from JavaScript (without making a request to the server)

In other words, what you have here doesn't make any sense.


I mentioned before that you can pass the HTML (client side) text box element into the function that calculates the average. From there you can use the .parentNode property of the HTML text box element to try and find the row that the text box belongs to.

In this case you don't even need to use the ClientID. You just pass a reference to the function like this:

C# code:
Expand|Select|Wrap|Line Numbers
  1.  txt1.Attributes["OnBlur"] = "CalcAverage(this)";
  2.  
Notice how I'm passing "this" to the CalcAverage method? "this" is JavaScript that refers to the HTML text box element.

You have to change your CalcAverage method so that it takes a parameter in order for you to be able do anything with the element passed into the CalcAverage method:

JavaScript code:
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript">
  2.   function CalcAverage(textBoxElement) {
  3.  
  4.   }
Now, from here you can do a lot of things to accomplish what you want.

You can try what I suggested with the .parentNode of the HTML text box element to try and find a reference to the row that the text box belongs to......

Or you could try retrieving a reference to the table that contains all of the text boxes, use the document.getElementsByTagName method to retrieve all of the rows in the table ("tr" elements)...and then use the document.getElementsByTagName method again grab all input elements... and then check if the row contains the text box element that caused the onblur event.

There are many ways to solve this problem.

Please re-read this thread (because I have said all of this already but I think you've ignored what I was saying...).

If you don't understand something that we are saying please either look it up using google or say that you don't understand and we'll try to explain.

-Frinny
Jan 14 '10 #26
Thats for the reply. I do understand. I have tried using "this" but keep getting the same errors. I understand you can not use the server controls in javascript and maybe did not word it correctly. But accessing the HTML for them is pretty much the same meaning "You have server controls and you can manipulte what they display on the client". But I am not trying to teach anyone. I have been looking for code to find the parentNote and find other controls in that row but so far have not had any success. I though I could build up the clientID of the .Net control but that does not work.

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript"> 
  2.   function CalcAverage(variable) { 
  3.  
  4. var element = "'" + '<' + '%' + '= ' + 'txt' + variable + '.ClientID' + ' %' + '>' + "'"
  5.   }
and call it like this

txtTotalSteps.Attributes["OnBlur"] = "CalcAverage('TotalSteps')";

But have not had success yet.......
Jan 14 '10 #27
Frinavale
Expert Mod 8TB
ClientID is a property of the ASP.NET TextBox control.

ClientID is not a valid property, or id of anything client side.

For example (C# code):
Expand|Select|Wrap|Line Numbers
  1.  aLabel.Text = txtTotalSteps.ClientID;
Will display something like "ctl00_cphBody_txtTotalSteps" in the label.
This string ("ctl00_cphBody_txtTotalSteps") is the ID attribute of the HTML <input type="text" /> that represents the ASP.NET TextBox (txtTotalSteps) in the browser.

Hmm..that is a little confusing...let me try to re-word this. Your ASP.NET TextBox control is rendered as an <input type="text" /> HTML element. It is given a unique id...."ctl00_cphBody_txtTotalSteps"... so that the page's HTML is valid and so that you can access this element using JavaScript.

When the ASP.NET TextBox is rendered as HTML it looks like:
Expand|Select|Wrap|Line Numbers
  1. <input type="text" id="ctl00_cphBody_txtTotalSteps" name="...."  onblur="CalculateAverage(this)"/>

Can you see how the following JavaScript doesn't make sense?
Expand|Select|Wrap|Line Numbers
  1. var element = "'" + '<' + '%' + '= ' + 'txt' + variable + '.ClientID' + ' %' + '>' + "'"
An element with the id "<%=txtTotalSteps.ClientID %>" doesn't exist on the page....but an element with an id of "ctl00_cphBody_txtTotalSteps" does. You need to use the "ctl00_cphBody_txtTotalSteps" ID in with the JavaScript document.getElementID() method to retrieve a reference to the HTML <input type="text"> element.

The way you can access the "ctl00_cphBody_txtTotalSteps" ID in your server code is through the ASP.NET TextBox's ClientID property.

You can write the value of this ID directly into your JavaScript when the page is being rendered by the server to be sent to the browser. You can either dynamically generate the JavaScript entirely in your C# code and register it with the page (as you did originally)...or you can write the JavaScript in your ASP code and use ASP syntax execute server-side code that will write the ClientID into the page as it's being rendered.

In ASP anything in <% %> is executed on the server....so to write the ClientID of the ASP.NET TextBox into the page you would have: <%Response.Write(txtTotalSteps.ClientID);%> or if you want to use the short hand version you can use <%=txtTotalSteps.ClientID;%>....When you look at the HTML source code in the browser you will notice that <%=txtTotalSteps.ClientID;%> is not there...but the text "ctl00_cphBody_txtTotalSteps" is in it's place. That is because everything between the <% %> was executed server side to generate the text that is displayed client side. You cannot use JavaScript code to try and dynamically generate ASP code since JavaScript runs client-side in the browser and ASP code is executed on the server before the page is sent to the browser.


Do you understand this before we start getting into the parentNode stuff?

-Frinny
Jan 14 '10 #28
Frinavale
Expert Mod 8TB
Here is a working example of a table that has 2 rows, each with 2 textboxes in them.
Each textbox calls the calculateAverage() method and passes a reference to itself into the method during the onblur event.

The calculateAverage() method uses the textbox element passed into it to find a reference to the row that the textbox belongs to. From there it retrieves all textboxes for the row and calculates the average. Once it's found the average it displays it in the <span> that is used to display the average (I picked a span because the ASP.NET Label control is rendered as a span).

This example is in pure HTML (not ASP.NET) If you want to see it working, copy the following code into a text document and save it as a .html file (I recommend turning off the line numbers by clicking the "Line Numbers" link before you copy it otherwise a bunch of "#" will appear when you paste what you've copied). After you've saved the file, close the text document and then double click on the HTML file (it should open in your default browser).

Please note that the calculateAverage() method creates JavaScript Number objects based on the values entered into the textboxes. That means that if you enter something that is not a number it will cause an error! It's a rough idea of what you want to do and you're going to have to clean it up so that it works smoothly. Also, please remember that JavaScript is case sensitive. So if you try to call CalculateAverage() it won't work since the name of the method is calculateAverage.


Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3.  
  4. <table border="1" id="textBoxTable">
  5.   <tr>
  6.     <th>Row</th>
  7.     <th>TextBox A</th>
  8.     <th>TextBox B</th>
  9.     <th>Average</th>
  10.   </tr>
  11.   <tr>
  12.     <td>Row1: </td>
  13.     <td><input type="text" id="txtA" onblur="calculateAverage(this)" value="0" /></td>
  14.     <td><input type="text" id="txtB" onblur="calculateAverage(this)" value="0"/></td>
  15.     <td><span id="avgForRow1"></span></td>
  16.   </tr>
  17.   <tr>
  18.     <td>Row2: </td>
  19.     <td><input type="text" id="txtA2" onblur="calculateAverage(this)" value="0" /></td>
  20.     <td><input type="text" id="txtB2" onblur="calculateAverage(this)" value="0"/></td>
  21.     <td><span id="avgForRow2"></span></td>
  22.   </tr>
  23. </table>
  24.  
  25. <script type="text/javascript">
  26. function calculateAverage(textBoxElement)
  27. {
  28.   var parentCell = textBoxElement.parentNode;
  29.   var row = parentCell.parentNode;
  30.   var inputs = row.getElementsByTagName("input");
  31.   var spanToDisplayAvg = row.getElementsByTagName("span")[0];
  32.   var i;
  33.   var sum = 0;
  34.   for(i=0; i<inputs.length; i++)
  35.   {
  36.     sum += Number(inputs[i].value);
  37.   }
  38.   var average = sum/inputs.length*100;
  39.   spanToDisplayAvg.innerHTML = average;
  40. }
  41. </script>
  42.  
  43. </body>
  44. </html>
Jan 14 '10 #29
I understand. I will test it out.
Thanks
Jan 14 '10 #30
amitdotchauhan
simplest way i would do is

i will registerclient script block like

Expand|Select|Wrap|Line Numbers
  1. str="function add() { ";
  2. str+="var TB1=document.getelementbyid('"+ textbox1.ClientID +"').value;";
  3. str+="var TB2=document.getelementbyid('"+ textbox2.ClientID +"').value; ";
  4. str+="var LB1=document.getelementbyid('"+ label1.ClientID +"'); ";
  5. str+="LB1.value=TB1+TB2;}"
once this is registered just i need to attach this function on textbox's blur event
i.e. set the onclientclick="return Add()"

Ready to go!!
Jan 23 '10 #31

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by kmunderwood | last post: by
1 post views Thread by Jungleman | last post: by
4 posts views Thread by Ryan Knopp | last post: by
3 posts views Thread by haakern | last post: by
reply views Thread by jasper98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.