1

Hello I am binding Javascript Function with Asp.net linkButton.
But i am facing the issue with calling functionality of the Javascript function.

function AddToDatabase(url) {
     alert(url);
}

  <asp:LinkButton ID="lnkBtn" CommandName ="sendLink" runat="server" 
       OnClientClick='javascript:AddToDatabase("' <%# Eval("myUrl") %> '")'  
        CommandArgument='<%# Eval("myUrl") %>' Text='<%# Eval("myUrl") %>'>'
          <%# Eval("myUrl") %>'</asp:LinkButton>

It shows me error Server tag not well formed.

1 Answer 1

0
 <asp:LinkButton ID="lnkBtn" CommandName ="sendLink" runat="server" 
   OnClientClick='javascript:AddToDatabase(<%# Eval("myUrl") %>)'  
    CommandArgument='<%# Eval("myUrl") %>' Text='<%# Eval("myUrl") %>'>
   </asp:LinkButton>

Edit 1

change OnClientClick as follows

  OnClientClick='<%# Eval("myUrl", "javascript:AddToDatabase({0});") %>'

combining both

   <asp:LinkButton ID="lnkBtn" CommandName ="sendLink" runat="server" 
            OnClientClick='<%# Eval("myUrl", "javascript:AddToDatabase({0});") %>' 
            CommandArgument='<%# Eval("myUrl") %>' Text='<%# Eval("myUrl") %>'>
  </asp:LinkButton>

References
Passing Eval parameter to a JavaScript function from ASPX file

Edit 2

Here is another link
Passing variables to javascript in onclientclick
passing a rowindex from gridview item click to javascript function

Edit 3

The one which helped answerer by the @moiz

   <asp:LinkButton ID="lnkBtn" CommandName ="sendLink" runat="server" 
        OnClientClick='<%# Eval("myUrl", "javascript:AddToDatabase(&#39;{0}&#39;);") %>' 
                 CommandArgument='<%# Eval("myUrl") %>' Text='<%# Eval("myUrl") %>'>
       '<%# Eval("myUrl") %>'
  </asp:LinkButton>
Sign up to request clarification or add additional context in comments.

5 Comments

It gives me error : SyntaxError: syntax error [Break On This Error] javascript:AddToDatabase(<%# Eval("myUrl") %>);
SyntaxError: missing ) after argument list [Break On This Error] javascript:AddToDatabase(google.com);
I have done that. thanks for the reply. <asp:LinkButton ID="lnkBtn" CommandName ="sendLink" runat="server" OnClientClick='<%# Eval("myUrl", "javascript:AddToDatabase(&#39;{0}&#39;);") %>' CommandArgument='<%# Eval("myUrl") %>' Text='<%# Eval("myUrl") %>'>'<%# Eval("myUrl") %>'</asp:LinkButton>
@Moiz I have edited my answer with some link which may help you.
Thanks shekhar. i have done that. thanks for your quick reply.

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.