inject JavaScript from code behind

A

Angel

Hi,

I have a page which shows a matrix of names and grades for given period. All
the grades fields must be editable and
they can be up to 200 fields (20 students with 10 grades each)
Since there are a lots of fields I do not want to go back and forth to
validate entered values on any change.
I decided that this may be achieved with some JavaScript functions to
validate some controls and to calculate average and total values of other
controls on change of any editable fields.
On change value of input fields JavaScript function will check if this is a
valid value an if so it will calculate values of average and total fields
and update their values.
I use Textbox web control, I also add Attribute("onChange","return
validate(string id) ) to control to make it call proper JavaScript
function.

The valid grades are loaded from Database and need to be injected at run
time in the client html.
My problem is that I do not know how to inject(insert) this values as an
array in the client html so my function will use this values to validate the
user entry;

Thanks
Angel
 
H

Hermit Dave

use StringBuilder to builder a string which will represent a
<script language=javascript>
// your javascript array definition here
</script>

StringBuilder sb = new StringBuilder();
sb.Appent("<script language=javascript>\n");
sb.Append(" var myArray[5];\n")
sb.Append("myArray[0] = " + serversideArray_or_list[0] + "\n");
// add more array items or use a for loop etc.
sb.Appent("</script>\n");
Page.RegisterClientsideScript("yourscriptreference", sb.ToString());

something like this

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
 
K

Karl Seguin

Or you could use the Page.RegisterArrayDeclaration

something like Page.RegisterArrayDeclaration("myArray", "1,2,3,4,5,6,7");

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Hermit Dave said:
use StringBuilder to builder a string which will represent a
<script language=javascript>
// your javascript array definition here
</script>

StringBuilder sb = new StringBuilder();
sb.Appent("<script language=javascript>\n");
sb.Append(" var myArray[5];\n")
sb.Append("myArray[0] = " + serversideArray_or_list[0] + "\n");
// add more array items or use a for loop etc.
sb.Appent("</script>\n");
Page.RegisterClientsideScript("yourscriptreference", sb.ToString());

something like this

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
Angel said:
Hi,

I have a page which shows a matrix of names and grades for given period. All
the grades fields must be editable and
they can be up to 200 fields (20 students with 10 grades each)
Since there are a lots of fields I do not want to go back and forth to
validate entered values on any change.
I decided that this may be achieved with some JavaScript functions to
validate some controls and to calculate average and total values of other
controls on change of any editable fields.
On change value of input fields JavaScript function will check if this
is
a
valid value an if so it will calculate values of average and total fields
and update their values.
I use Textbox web control, I also add Attribute("onChange","return
validate(string id) ) to control to make it call proper JavaScript
function.

The valid grades are loaded from Database and need to be injected at run
time in the client html.
My problem is that I do not know how to inject(insert) this values as an
array in the client html so my function will use this values to validate the
user entry;

Thanks
Angel
 
H

Hermit Dave

oh... never had to use one.. hmmm keep that in mind next time :)

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
Karl Seguin said:
Or you could use the Page.RegisterArrayDeclaration

something like Page.RegisterArrayDeclaration("myArray", "1,2,3,4,5,6,7");

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Hermit Dave said:
use StringBuilder to builder a string which will represent a
<script language=javascript>
// your javascript array definition here
</script>

StringBuilder sb = new StringBuilder();
sb.Appent("<script language=javascript>\n");
sb.Append(" var myArray[5];\n")
sb.Append("myArray[0] = " + serversideArray_or_list[0] + "\n");
// add more array items or use a for loop etc.
sb.Appent("</script>\n");
Page.RegisterClientsideScript("yourscriptreference", sb.ToString());

something like this

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
Angel said:
Hi,

I have a page which shows a matrix of names and grades for given
period.
All
the grades fields must be editable and
they can be up to 200 fields (20 students with 10 grades each)
Since there are a lots of fields I do not want to go back and forth to
validate entered values on any change.
I decided that this may be achieved with some JavaScript functions to
validate some controls and to calculate average and total values of other
controls on change of any editable fields.
On change value of input fields JavaScript function will check if this
is
a
valid value an if so it will calculate values of average and total fields
and update their values.
I use Textbox web control, I also add Attribute("onChange","return
validate(string id) ) to control to make it call proper JavaScript
function.

The valid grades are loaded from Database and need to be injected at run
time in the client html.
My problem is that I do not know how to inject(insert) this values as an
array in the client html so my function will use this values to
validate
the
user entry;

Thanks
Angel
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top