Javascript into ASP.Net Simple example wanted.. Sum Textboxes client side

D

Davisro

I would like to have a total box show the totol of four textboxes when
anyone of them change.

I know I could do this via postback, but would like to do this on client
side utilizing javascript.

I am familiar with asp.net but not in how to migrate javascript into the
html.

Thanks,

Rog
 
C

Cowboy \(Gregory A. Beamer\) [MVP]

Here is one idea (will need tweaking):

<input type="text" id="sumBox" name="sumBox" onFocus="SumBoxes();" />


<script language="JavaScript">
function SumBoxes()
{
var val1 = form1.addBox1.Value;
var val2 = form2.addBox1.Value;
var val3 = form3.addBox1.Value;
var val4 = form4.addBox1.Value;

//May need test for "" on each box here, turn to 0, like
if(val1 == '')
{
val1 = 0;
}

form1.sumBox.Value = val1 + val2 + val3 + val4;
}
</script>

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************************************************
Think outside the box!
***************************************************************
 
C

Curt_C [MVP]

if you want it with clientside javascript I suggest you look at a clientside
javascript newsgroup.
you'll probably have to catch the keydown/keyup in the boxes.
 
D

Davisro

I would like to use WebForm Textboxes as I have a lot of .Net code that
relates to them. Is this possible in mostly the same way you stated above?

Thanks,

Rog
 
B

Bruno Sirianni

For insert JavaScript on client page you can use RegisterClientScriptBlock
or RegisterStartupScript method.
string s = "<script language=\"JavaScript\">\n" +
"function SumBoxes()\n" +
"{\n" +
............
"}\n" +
"</script>\n";

RegisterClientScriptBlock("SUM", s);

for attach this function to your textbox you can use this code :
txtBox1.Attributes["OnChange"] = "SumBoxes();";

Brun
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,249
Latest member
KattieCort

Latest Threads

Top