passing a string from the server to client side javascript

G

Guest

Hello all,
I have an ASP.NET website where one of my pages contain javascript that is a
check to see if anydata within a datagrid has changed. My problem is that it
takes a considerable amount of time to populate the array in the javascript
after the datagrid is populated.

What I am thinking of doing is creating a comma delimited string on the
server side (.vb) and then have the javascript pick it up through array()....

My problem that I am trying to figure out is how do I pass a string that is
created in a server side function back to the client so that a javascript
function could read it and populate an array.

Does anybody have any pointers how to do this?

Thank you,
Lyners
 
M

Maarten

In your aspx file:

<%# MyString%>

In your VB-file:
Protected MyString as string = "m,y,s,t,r,i,n,g"

Hope this helps you
 
W

Winista

Prepare the commans separated string and then write it in JS block using
RegisterclientScriptBlock kind of methods. And then have another JS function
implementted that picks up this string and populate what ever you are
populating.
 
G

Guest

Thanks Maarten, but my string keeps coming back empty. I had a simular setup
prior to your comment, I changed the Public string to a protected string, but
still no dice.

Here is my declaration and my statement that is creating the string:

Protected strIds As String = "Start"

In my loop I do the following;

dblRecordCounter = dblRecordCounter + 1
strIds = strIds & "," & "MyDataGrid__ctl" + Str(dblRecordCounter
+ 2) + "_txtField"

In the aspx file I have this;
ServerString = new String('<%# strIds%>');
var ids = ServerString.split(',');

but ids[0] or 1,2,3,4,5.... is blank

I know I am missing something simple. Can you see it?

Thank you,
Lyner
 
S

sirfunusa

You are not sending the string to the client.

In your client html

function Page_onLoad() {
var blnVisible = <%= m_strVisible %>;
 
G

Guest

I knew it was something like this, but another question, because I still
don't have it working. Does the javascript fire off the Page_onLoad()
everytime the page is loaded? Because I put an alert in the onLoad part and
it never fired, which leads me to think that in the body tag I need to put an
onLoad event to pick up the string.

Correct or not?

Thans alot!
 
E

Eliyahu Goldin

This is normally done with a hidden input control:

<input type=hidden runat=server id=inhMyParameter">

Eliyahu
 
S

sirfunusa

Incorrect. Perhaps you posted to the wrong question? He was asking how
to pass a string from server to client.
 
W

Winista

Not incorrect at all. Use of hidden field is another approach to do the same
thing.
 

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,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top