简体   繁体   中英

Passing JavaScript parameters through ASP.NET ListView

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?

当属性包含服务器代码时,应使用单引号'而不是双引号"

<asp:Label ID="label1" runat="server" OnClientClick='<%#Eval("Foo")%>' />

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM