Passing Server side variable to JavaScript?

  • Thread starter Thread starter rockdale
  • Start date Start date
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
 
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("'", "\\'") +
"');");
 
Back
Top