0

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?

3
  • Have you tried using Eval() isntead of Container.DataItem? This link is is some kind of databound control, right? Commented Aug 25, 2011 at 16:14
  • @James, you are correct, it is databound Commented Aug 25, 2011 at 16:17
  • don't you need an space? like <% #Co... and ...) %> Commented Aug 25, 2011 at 16:31

3 Answers 3

2

Got it working like this:

<asp:HyperLink ID="lnkIDNum" 
           runat="server" 
           NavigateUrl=<%# "javascript:ChangeLoc('ChangeView', '" + Container.DataItem("IDNum") + "')" %>>
     <%#Container.DataItem("IDNum")%>
</asp:HyperLink>
Sign up to request clarification or add additional context in comments.

3 Comments

When I use your code, I get "Exception Details: System.FormatException: Input string was not in a correct format."
Are you sure its caused the NavigateUrl? Try this: Container.DataItem("IDNum").ToString() in the NavigateUrl assignment.
I do believe we have a winner !!.. Thanks @Mrchief
1

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>

2 Comments

Even with the Eval function, same message
There's something else wrong with your code then. This shouldn't produce any errors.
1

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>

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.