Skip to content Skip to sidebar Skip to footer

Passing A Serverside Variable To A Javascript Function In Asp.net

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

Solution 1:

Got it working like this:

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

Solution 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>

Solution 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:HyperLinkID="lnkIDNum"runat="server"></asp:HyperLink>

Post a Comment for "Passing A Serverside Variable To A Javascript Function In Asp.net"