Passing Server side variable to JavaScript?

R

rockdale

Hi, All:

How can I achieve this?

I have a Function in JavaScript and this function requires an argument
which retruns from a backend C# function.

I tried the following, but keep getting error "CS1525: Invalid
expression term '<' "

I remeber we can do something like this in ASP. ?

<asp:linkbutton id="lbnQuery"
onClick="myJavaScriptFunc(<%=CSharpFuncReturnsString()%>)"
CssClass="NormalLinkSmall" runat="server" Text="Query"
BorderStyle="none"></asp:linkbutton>

Thanks for your help
-Rockdale
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Set the event from codebehind:

lbnQuery.Attributes.Add("onclick", "myJavaScriptFunc(" +
CSharpFuncReturnsString() + ");");

If the CSharpFuncReturnsString returns a string that contains something
that isn't a number, you have to put apostrophes around the value. And
if the value can contain apostrophes or backslashes, you have to escape
them by replacing \ with \\ and ' with \':

lbnQuery.Attributes.Add("onclick", "myJavaScriptFunc('" +
CSharpFuncReturnsString().Replace("\\", "\\\\").Replace("'", "\\'") +
"');");
 

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,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top