Passing a serverside variable to a javascript function in asp.net

情到浓时终转凉″ 提交于 2020-01-16 19:13:33

问题


I need to pass a server side variable to my javascript function such as below:

<asp:HyperLink ID="lnkIDNum" runat="server" NavigateUrl="javascript:ChangeLocView('ChangeView', '<%#Container.DataItem("IDNum")%>')"><%#Container.DataItem("IDNum")%></asp:HyperLink>  

I get an error at the serverside variable being passed into the Javascript function.

Parser Error Message: The server tag is not well formed.

Can anyone assist?


回答1:


Got it working like this:

<asp:HyperLink ID="lnkIDNum" 
           runat="server" 
           NavigateUrl=<%# "javascript:ChangeLoc('ChangeView', '" + Container.DataItem("IDNum") + "')" %>>
     <%#Container.DataItem("IDNum")%>
</asp:HyperLink>



回答2:


Try using the Eval() function instead, and do something like this:

<asp:HyperLink ID="lnkEdit" runat="server" NavigateUrl="someFunc('ChangeView', '<%#Eval("SomeColumn")%>');"><%#Eval("SomeColumn")%></asp:HyperLink>



回答3:


You probably should set the attributes in the code behind.

string idNum = Container.DataItem("IDNum");
lnkIDNum.NavigateUrl = 
    "javascript:ChangeLocView ('ChangeView', '" 
  + idNum 
  + "'";
lnkIDNum.Text = idNum;

and in the aspx:

<asp:HyperLink ID="lnkIDNum" runat="server"></asp:HyperLink>


来源:https://stackoverflow.com/questions/7193464/passing-a-serverside-variable-to-a-javascript-function-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!