server tag not well formed error

971 Views Asked by At

I know there are a lot of threads about this error, but I've looked through all of them and haven't been able to make it work. It's probably something very small that I am overlooking.

This is the code I have now, in an asp:Button component

<asp:Button ID="btn_profile" runat="server" Text="Bekijk de wenslijst"
CssClass="wenslijst_preview_btn" 
OnClick="btn_goToChild_Click(<%# Eval("pk_child_id") %>)"/>

I have tried multiple things, for example the following:

OnClick='btn_goToChild_Click(<%# Eval("pk_child_id") %>)'

But then I get the following error:

preprocessor directives must appear as the first non-whitespace character on a line

If you need to see more code, let me know!

4

There are 4 best solutions below

2
On BEST ANSWER
<asp:Button ID="btn_profile" runat="server" Text="Bekijk de wenslijst"
 CssClass="wenslijst_preview_btn" CommandArgument='<%# Eval("pk_child_id") %>'
 OnClick="btn_goToChild_Click"/>
  protected void btn_goToChild_Click(object sender, EventArgs e)
    {
        Button btn= (Button) sender;
        if (btn==null) return;
        var id =btn.CommandArgument;
         // Or int id=(int)btn.CommandArgument;
    }
0
On

OnClick is a server-side event, so should just be the name of a server-side event handler. OnClientClick (where available) is a client-side event handler, so would take a JavaScript expression to evaluate.

Try

  <asp:Button ID="Button1" runat="server" Text="Button"
             OnClientClick='btn_goToChild_Click(<%# Eval("pk_child_id") %>)' /> 
0
On

If you want to use Javascript expression, use OnClientClick not OnClick. These are different side events.

0
On

OnClick is used for server side methods only, for client side calls you should use "OnClientClick" attribute.