Wednesday, March 21, 2012
passing a string from the server to client side javascript
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,
LynersPrepare 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.
"Lyners" <Lyners@.discussions.microsoft.com> wrote in message
news:DF2717EF-9B7F-4CE7-B78E-973878F7092C@.microsoft.com...
> 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
>
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
"Lyners" <Lyners@.discussions.microsoft.com> wrote in message
news:DF2717EF-9B7F-4CE7-B78E-973878F7092C@.microsoft.com...
> 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
>
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, bu
t
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
"Maarten" wrote:
> 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
> "Lyners" <Lyners@.discussions.microsoft.com> wrote in message
> news:DF2717EF-9B7F-4CE7-B78E-973878F7092C@.microsoft.com...
>
>
You are not sending the string to the client.
In your client html
function Page_onLoad() {
var blnVisible = <%= m_strVisible %>;
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 a
n
onLoad event to pick up the string.
Correct or not?
Thans alot!
"sirfunusa" wrote:
> You are not sending the string to the client.
> In your client html
> function Page_onLoad() {
> var blnVisible = <%= m_strVisible %>;
>
This is normally done with a hidden input control:
<input type=hidden runat=server id=inhMyParameter">
Eliyahu
"Lyners" <Lyners@.discussions.microsoft.com> wrote in message
news:DF2717EF-9B7F-4CE7-B78E-973878F7092C@.microsoft.com...
> 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
>
Incorrect. Perhaps you posted to the wrong question? He was asking how
to pass a string from server to client.
Why do you think is it incorrect?
Eliyahu
"sirfunusa" <sirfunusa@.hotmail.com> wrote in message
news:1144081480.686619.283100@.i39g2000cwa.googlegroups.com...
> Incorrect. Perhaps you posted to the wrong question? He was asking how
> to pass a string from server to client.
>
Not incorrect at all. Use of hidden field is another approach to do the same
thing.
"sirfunusa" <sirfunusa@.hotmail.com> wrote in message
news:1144081480.686619.283100@.i39g2000cwa.googlegroups.com...
> Incorrect. Perhaps you posted to the wrong question? He was asking how
> to pass a string from server to client.
>
passing a string from the server to client side javascript
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,
LynersPrepare 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.
"Lyners" <Lyners@.discussions.microsoft.com> wrote in message
news:DF2717EF-9B7F-4CE7-B78E-973878F7092C@.microsoft.com...
> 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
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
"Lyners" <Lyners@.discussions.microsoft.com> wrote in message
news:DF2717EF-9B7F-4CE7-B78E-973878F7092C@.microsoft.com...
> 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
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
"Maarten" wrote:
> 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
> "Lyners" <Lyners@.discussions.microsoft.com> wrote in message
> news:DF2717EF-9B7F-4CE7-B78E-973878F7092C@.microsoft.com...
> > 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
>
You are not sending the string to the client.
In your client html
function Page_onLoad() {
var blnVisible = <%= m_strVisible %>;
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!
"sirfunusa" wrote:
> You are not sending the string to the client.
> In your client html
> function Page_onLoad() {
> var blnVisible = <%= m_strVisible %>;
>
This is normally done with a hidden input control:
<input type=hidden runat=server id=inhMyParameter"
Eliyahu
"Lyners" <Lyners@.discussions.microsoft.com> wrote in message
news:DF2717EF-9B7F-4CE7-B78E-973878F7092C@.microsoft.com...
> 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
Incorrect. Perhaps you posted to the wrong question? He was asking how
to pass a string from server to client.
Why do you think is it incorrect?
Eliyahu
"sirfunusa" <sirfunusa@.hotmail.com> wrote in message
news:1144081480.686619.283100@.i39g2000cwa.googlegr oups.com...
> Incorrect. Perhaps you posted to the wrong question? He was asking how
> to pass a string from server to client.
Not incorrect at all. Use of hidden field is another approach to do the same
thing.
"sirfunusa" <sirfunusa@.hotmail.com> wrote in message
news:1144081480.686619.283100@.i39g2000cwa.googlegr oups.com...
> Incorrect. Perhaps you posted to the wrong question? He was asking how
> to pass a string from server to client.
You never set the value.
Passing a string to a client side function
1. Put the xml string in a textbox control (and set the display style to
none using css stylesheet) then simply reference the value of your textbox
control, to get the XML string.
2. (Probably the better one) Add a hidden input field to your form, set the
runat attribute to server and then simply assign your XML string as the
value of the hidden input field from within your code behind. Using this
method the encoding is taken care of for you as the whole lot will be html
encoded. (I pretty sure this should then work fine, but I may be wrong!!)
Hope one of the above works for you.
Matt
http://www.3internet.com
"Jorell" <anonymous@dotnet.itags.org.discussions.microsoft.com> wrote in message
news:70A4100B-D764-4FEA-B111-A5B9AE9E6529@dotnet.itags.org.microsoft.com...
> Hi everyone,
> I have an XML that I create on the fly. I need to pass this to a
Javascript function and I do it in this way:
> Page.RegisterStartupScript("Test", "<script
language=""javascript"">printHidden('" & XML & "')</script>")
> This works however if you encase the XML in single quotes then there could
possibly a terminating ' in the XML and on the other hand if you encase the
XML in double quotes and there is a single " in the XML...this becomes the
terminator and the client side throws an error: Unterminated string constant
> The user enters the information the XML is built with and we must allow
them the ability to enter these any characters.
> What I need to do is encode the string server side before it is passed and
then unencode it on the client.
> I have attempted to use Regex.Escape on the Server then unescape() on the
client with no luck.
> Any ideas/suggestions would be great! Thank you
> JorellJorell,
If you use the option of outputting your text to a form variable you should
not need to use escape characters at all. If you just leave the string
exactly as you want it to appear and output it to the form variables, when
you retrieve the value you should see exactly what you are looking for. Or
is there something that I am missing as to why do you need to put escape
characters in?
Matt
http://www.3internet.com
"Jorell" <anonymous@.discussions.microsoft.com> wrote in message
news:B8A31C70-DF4A-49D1-AB87-CA7E3AF4487D@.microsoft.com...
> Hi there. Thank you for the suggestion. I tried this as I figured the same
however the value, innerText and innerHtml etc properties all return the
HTML but with the escape characters ie. \"
> This still causes and issue and without actually doing a loop to look for
escape characters and replacing them...but there is a possibly of not
catching all of them.
> Essentially I am just writing this HTML to a div using the document.write
method and then printing the div using execWB.
> Any ideas would be great thank you!
> Jorell
Hi there Matt
I actually don't want the characters in the string. What I have is Complex HTML that I have created in a server function
I have a js function which needs to take that HTML and write it to the Body of an IFrame that it creates. ( that can't be changed) So basically what I need to be able to do is end up with a variable inside that js function which is straight HTML no escapes.
Mostly the problem is if you use textboxes of any sort...the innerHtml, innerText, Text, or value fields always return the value with escape charaters when accessing them from the client. I am not sure why
So no matter what I put the HTML in, it adds escape chars
Any ideas? I appologize if it is unclear! Thank yo
Jorell
Passing a string to a client side function
character (e.g. \' or \"). The backslash is the escape character in
JavaScript.
--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
"Jorell" <anonymous@dotnet.itags.org.discussions.microsoft.com> wrote in message
news:70A4100B-D764-4FEA-B111-A5B9AE9E6529@dotnet.itags.org.microsoft.com...
> Hi everyone,
> I have an XML that I create on the fly. I need to pass this to a
Javascript function and I do it in this way:
> Page.RegisterStartupScript("Test", "<script
language=""javascript"">printHidden('" & XML & "')</script>")
> This works however if you encase the XML in single quotes then there could
possibly a terminating ' in the XML and on the other hand if you encase the
XML in double quotes and there is a single " in the XML...this becomes the
terminator and the client side throws an error: Unterminated string constant
> The user enters the information the XML is built with and we must allow
them the ability to enter these any characters.
> What I need to do is encode the string server side before it is passed and
then unencode it on the client.
> I have attempted to use Regex.Escape on the Server then unescape() on the
client with no luck.
> Any ideas/suggestions would be great! Thank you
> JorellI agree however I was kind of looking at that as a last resort. If possible I was hoping there was functions on both sides that I had missed that would take care of this for me. Any chance this is possible? Thank you again
Jorell
Friday, March 16, 2012
Passing a value from server side to client side
in data in a text box then I need to pass that data to the client side.
The problem Im having is I can't seem to get the value from the text
box be it a regular asp.net text box or even when I assign a value in a
hidden textbox I still can't seem to get the value. I thought using:
document.getElementbyID("Client_ID").innerText would get the value but
its not.
Any help will be greatly appreciated.
ThanksWhere is the textbox? Grid/DataList/Repeater? User control? Content page?
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
<kimberly.walker@.consultant.comwrote in message
news:1152715225.255729.148180@.b28g2000cwb.googlegr oups.com...
Quote:
Originally Posted by
Im having a bit of a problem here, what I want to do is a user types
in data in a text box then I need to pass that data to the client side.
The problem Im having is I can't seem to get the value from the text
box be it a regular asp.net text box or even when I assign a value in a
hidden textbox I still can't seem to get the value. I thought using:
document.getElementbyID("Client_ID").innerText would get the value but
its not.
Any help will be greatly appreciated.
Thanks
>
Kimberly,
The first thing to check is whether or not you have the correct client id.
ViewSource on the page and double check what client id your control is being
rendered with.
Regards,
--
S. Justin Gengo
Web Developer / Programmer
Free code library:
http://www.aboutfortunate.com
"Out of chaos comes order."
Nietzsche
<kimberly.walker@.consultant.comwrote in message
news:1152715225.255729.148180@.b28g2000cwb.googlegr oups.com...
Quote:
Originally Posted by
Im having a bit of a problem here, what I want to do is a user types
in data in a text box then I need to pass that data to the client side.
The problem Im having is I can't seem to get the value from the text
box be it a regular asp.net text box or even when I assign a value in a
hidden textbox I still can't seem to get the value. I thought using:
document.getElementbyID("Client_ID").innerText would get the value but
its not.
Any help will be greatly appreciated.
Thanks
>
Hi Eliyahu,
The text box is an asp.net text on a user control
Eliyahu Goldin wrote:
Quote:
Originally Posted by
Where is the textbox? Grid/DataList/Repeater? User control? Content page?
>
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
>
<kimberly.walker@.consultant.comwrote in message
news:1152715225.255729.148180@.b28g2000cwb.googlegr oups.com...
Quote:
Originally Posted by
Im having a bit of a problem here, what I want to do is a user types
in data in a text box then I need to pass that data to the client side.
The problem Im having is I can't seem to get the value from the text
box be it a regular asp.net text box or even when I assign a value in a
hidden textbox I still can't seem to get the value. I thought using:
document.getElementbyID("Client_ID").innerText would get the value but
its not.
Any help will be greatly appreciated.
Thanks
You should use the textbox ClientID property. You need to pass it from
server to client. If you do it in a hidden input control, it may look like:
<input type="hidden" id="clientId" runat="server"/>
...
var clientId=document.getElementById("clientId").value;
...
myText=document.getElementById(clientId).value
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
<kimberly.walker@.consultant.comwrote in message
news:1152759520.814547.273090@.m79g2000cwm.googlegr oups.com...
Quote:
Originally Posted by
Hi Eliyahu,
The text box is an asp.net text on a user control
>
Eliyahu Goldin wrote:
Quote:
Originally Posted by
>Where is the textbox? Grid/DataList/Repeater? User control? Content page?
>>
>Eliyahu Goldin,
>Software Developer & Consultant
>Microsoft MVP [ASP.NET]
>>
><kimberly.walker@.consultant.comwrote in message
>news:1152715225.255729.148180@.b28g2000cwb.googlegr oups.com...
Quote:
Originally Posted by
Im having a bit of a problem here, what I want to do is a user types
in data in a text box then I need to pass that data to the client side.
The problem Im having is I can't seem to get the value from the text
box be it a regular asp.net text box or even when I assign a value in a
hidden textbox I still can't seem to get the value. I thought using:
document.getElementbyID("Client_ID").innerText would get the value but
its not.
Any help will be greatly appreciated.
Thanks
>
>