0

I'm writing a bit of aspx code to call a JavaScript function from a server control. Here's the JavaScript:

<script type="text/javascript">
    function checkInfoPortalUnpin(portalareaid) {
        if(portalareaid == 1) {
            alert('a message');
        }
    }
</script>

And here's the calling aspx code:

<asp:ImageButton OtherFields="omitted..."
   OnClientClick='checkInfoPortalUnpin(<%# Eval("PortalAreaID") %>);' />

When I view the source of that line in the browser (IE8) it is rendering as this:

... onclick="checkInfoPortalUnpin(&lt;%# Eval(&quot;PortalAreaID&quot;) %>);"

and I get a syntax error when I click on the ImageButton. I know the OnClientClick does work because if I replace the <%# ... %> with a hard-coded '1' the function runs fine. Am I missing something?

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Arj
  • 1,981
  • 4
  • 25
  • 45
  • 2
    http://stackoverflow.com/questions/528066/server-tag-in-onclientclick Cheers. – vtortola Sep 19 '11 at 10:50
  • 1
    See this post http://stackoverflow.com/questions/6203200/how-to-call-javascript-function-from-asp-net-button-click-event – Saket Sep 19 '11 at 10:52

2 Answers2

1

Use this :

... OnClientClick="checkInfoPortalUnpin('<%# Eval(\"PortalAreaID\") %>');" ...

Saket
  • 45,521
  • 12
  • 59
  • 79
0

Just use:

<asp:ImageButton runat="server" OnClientClick="checkInfoPortalUnpin('<%# Eval(\"PortalAreaID\") %>');" />

You forget to put single quote outside the server tag.

Rased Dot Net
  • 530
  • 3
  • 14