Calculating Sum of Textbox Values via Client-Side Code in ASP.net User Controls

Posted on 2009-05-28
Medium Priority
Last Modified: 2012-08-14

I am creating a simple user control that will have 4 textboxes stacked on top of each other and then a label underneath that will calculate the sum of the textboxes with client-side code.  I know that you can essentially package javascript with a user control with the RegisterClientScript method but I'm having trouble figuring out how I can write the code so that I can have multiple controls on the page and each control will know to populate their total labels with the values from only their textboxes.  I would also like to be able to do it without having to register multiple versions of the same client script with references to different HTML controls in them.  Any thoughts on how I might achieve this?


Question by:MClarkCCR
  • 2
  • 2

Accepted Solution

cybergroupdoug earned 2000 total points
ID: 24493219
Use a regular javascript function (or functions), and bind the onchange JS event to that function.  The trick is that where you need to reference the .net controls, you need to put code like the attached snippet in place to get the correct value.
Document.getElementById('<%= DropDownList1.ClientID %>')

Open in new window


Author Comment

ID: 24494050

I can see how that works, so lets say in the codebehind of my User Control I will do a RegisterClientScript for a function called something like

function SumTextBoxes(txt1, txt2, txt3, txt4, lblSum)
  // Add values of textboxes and assign to lblSum

that accepts the relevant controls as parameters and then register the function in the onChange event of every textbox ( i.e. onChange="SumTextBoxes(<%= txt1.ClientID %>, etc, etc, etc, etc);") and that should work, right?

Also, since I am using RegisterClientScript, each time I load a new control when it tries to register its clientscript it will see that it was previously added by another control and not create duplicate javascript, correct?


Expert Comment

ID: 24494365
I'm not sure about whether you'd get duplicate js with RegisterClientScript.

What I was referring to was putting the JS function in the .aspx file (like you would a normal HTML page), not the code-behind.

Author Comment

ID: 24494639
I'm pretty sure that it wouldn't register it again, since you specify a keyname for the script when you register.  I'll just have to try it out to be sure.  The only reason I want to register the JS in the codebehind is so that anyone who uses the control in the future doesn't have to include a .js file with it or add the script to the top of the page.  It packages it all nice and neat together :) .  At any rate, I think I've got a good start to work with.  Thanks for your help!


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
There is a wide range of advantages associated with the use of ASP.NET. This is why this programming framework is used to create excellent enterprise-class websites, technologies, and web applications.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question