0

I'm trying to pass parameters to a JavaScript function from a link button in a ListView the function only alerts the the passed parameters so I used Eval("Value") as my parameter but it seems that I'm making a mistake.

My code for passing the parameter is

<asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="hello(<%#string.Concat(Eval("Longitude"),",",Eval("Latitude")) %>);">LinkButton</asp:LinkButton>

and the alert function is very simple

 function hello(x){
    alert(x);
}

and the server tag is still not well formed. Any ideas?

2 Answers 2

0

You should use single-quotes ' instead of double-quotes " when a property contains server-code.

<asp:Label ID="label1" runat="server" OnClientClick='<%#Eval("Foo")%>' />
Sign up to request clarification or add additional context in comments.

4 Comments

@Islam also try to surround the parameters for the JavaScript-function with quotes: hello("<%#Eval("")%>"). You should really use a JavaScript-console to debug. At least check the source of the page to make sure the HTML is well formed.
I'm sure that the HTML isn't well formed, but I cannot figure the problem so I posted my code to discuss it.
@Islam If the code compiles, you should be able to find the problem through a JavaScript-console.
It is fine, the solution is here stackoverflow.com/questions/249926/…
0

Your code should work, but, it looks like the binding mechanism is confused. I have struggled with this before and came up with the following work-around.

  1. Add a method to your code behind page.

    protected string CreateJavascriptCall(object longitude, object latitude) { return String.Format("hello({0},{1});return false;", longitude, latitude); }

  2. Change your aspx page code to:

    asp:LinkButton ID="LinkButton1" runat="server" OnClientClick='<%#CreateJavascriptCall(Eval("Longitude"), Eval("Latitude"))%>'>LinkButton

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.