insert server side code in javascript
dear all
i want to create Html page in the server side in aspx file (response.write("<input type = text name="tmp">") etc.... and i want also to read an array in the server side and to write it into javascript code which will be in the html which was written in the server side. how can i insert into javascript this text which was init in the serverside
var famname = new Array(3)
famname[0] = "some text that will put from sql_server - in server side"
famname[1] = "some text that will put from sql_server - in server side"
famname[2] = "some text that will put from sql_server - in server side"
document.form_name.tmp.value = famname[0];
how can i put this text in the HTML that i created in the server side.
in general my goal is to read data from SQL_server - and transform this data into a javascript that manipulate the data in the client sede
a demo which reads a string from the server and put it in javascript whice executes in the client side will be very good for me
thanks alot
Re: insert server side code in javascript
If you want to write and excute the client side script then use Page.RegisterStartupScript [c#]/RegisterStartupScript(key, script) [VB.Net] otherwise use Page.RegisterClientScriptBlock [C#]/RegisterClientScriptBlock(key, script) [VB.Net]
Re: insert server side code in javascript
how then the script will be written in the serverside?
Re: insert server side code in javascript
The "script" variable will hold your javascript. You can generate that in server side once you've retrieved all your data.
Re: insert server side code in javascript
first thnks for the quick reply.
ok how to retrieve the script text or to generate it in the server side i know - my problem is how to plant it the client side after i generated it and how to cause it to work.
Some very simple code example will very help me
Re: insert server side code in javascript
I can't show you a full fledged example right now, but here is the simplest example of writing:
VB Code:
Page.RegisterClientScriptBlock("script111", "<script language=javascript>MenuHiders(2);</script>")
In your server side code, loop through your dataset, and for each of the values you want from SQL Server, generate part of your javascript string, such as this:
VB Code:
strJS &= "famname[0] = " & ds.Tables(0).Rows(i).Item(4).ToString() & ";"
where i represents each row of the dataset.
You could declare your array before that code like so:
VB Code:
strJS &= "var famname = new Array(" & ds.Table(0).Rows.Count - 1 & ");"
Re: insert server side code in javascript
he he thanks - that close to what i mean.
does those lines should be written in this section
<%@ Page Language="vb" %>
<html>
<head>
<script language = vb runat =server>
...
Page.RegisterClientScriptBlock("script111", "<script languegege=javascript>MenuHiders(2);</script>")
...
strJS &= "famname[0] = " & ds.Tables(0).Rows(i).Item(4).ToString() & ";"
...
strJS &= "var famname = new Array(" & ds.Table(0).Rows.Count - 1 & ");"
.....
</script>
</head>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
...
<input type = text..."> etc....
...
</form>
</body>
</html>
Re: insert server side code in javascript
Yes, or in the codebehind. (I'm using VS 2003 EA)
Re: insert server side code in javascript
thanks alot!!!
you helped me alot.
if you just will need anything for me just let me know :)
if there will be problems ill let you know
Re: insert server side code in javascript
Quote:
Originally Posted by ronlahav
if you just will need anything for me just let me know :)
You sure about that? :p :afrog:
insert server side code in javascript
in my country there is kind of expression "till the half of the kingdom" - it means not every thing but close to that.
:)
Re: insert server side code in javascript
Which country would that be?
Re: insert server side code in javascript
you hear about this country alot in news - Israel