Search code examples
c#asp.netservertag

server tag not well formed error


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!


Solution

  • <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;
        }