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.

dda
  • 6,030
  • 2
  • 25
  • 34
snaplemouton
  • 1,459
  • 14
  • 28

1 Answers1

5

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

OnClientClick='<%# @"callSubClass("""+Eval("SubClassName").ToString()+@""");return false;" %>'
Amit Singh
  • 8,039
  • 20
  • 29
  • It works thank you. :) But can you explain me the part with the @" " please? – snaplemouton May 10 '13 at 17:24
  • @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 http://stackoverflow.com/questions/556133/whats-the-in-front-of-a-string-in-c – Amit Singh May 10 '13 at 17:29
  • @snaplemouton welcone always – Amit Singh May 10 '13 at 18:03