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

  • Thread starter Thread starter Davisro
  • Start date Start date
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
 
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!
***************************************************************
 
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.
 
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
 
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
 
Back
Top