2

I'm trying to call a javascript function.

function callSubClass(SubClassName) {
  ...
}

But I am trying to call it inside a repeater where the parameter "SubClassName" is a string bound to my repeater.

<asp:Repeater ID="rptSubClass" runat="server">
  <ItemTemplate>
    <asp:LinkButton Text='<%# Eval("SubClassName") %>' runat="server" ID="linkHomeItem" OnClick='<%# callSubClass(Eval("SubClassName")); return false; %>'/>
    <br />
  </ItemTemplate>
</asp:Repeater>

But every time I run it I get a compiler error saying:

CS1040: Preprocessor directives must appear as the first non-whitespace character on a line Error

Now I know the error is on the OnClick='stuff' but I've tried all I could think of to make it work. Here's a list of what I tried so far.

OnClick='<%# callSubClass(Eval("SubClassName")); return false; %>'
OnClick='callSubClass(<%# Eval("SubClassName") %>); return false;'
OnClick='"callSubClass("+<%# Eval("SubClassName") %>+"); return false;"'

What am I doing wrong? Thanks for any help provided.

1 Answer 1

5

Try Like this...Instead of Onclick use OnClientClick to run javascript code.....

OnClientClick='<%# @"callSubClass("""+Eval("SubClassName").ToString()+@""");return false;" %>'
Sign up to request clarification or add additional context in comments.

3 Comments

It works thank you. :) But can you explain me the part with the @" " please?
@snaplemouton @ is used in string to escape the sqequence and to include double quote you in your string.. have to write @"""" it will print " like this here is link which explain in technical term stackoverflow.com/questions/556133/…
@snaplemouton welcone always

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.