Mike
Mike

Reputation: 417

Server tag is not well formed error on GridView row LinkButton

I'm getting this error and I can't for the life of me figure out why. Basically, I've got a LinkButton on each row of a GridView control that should delete the record associated with that row when clicked. I've used the Container.DataItem before successfully with hrefs, but haven't tried before with a LinkButton. The link should pass the GridView's DataKey to a server-side function that deletes the record from the database. Any help is greatly appreciated! Here is the relevant code:

<asp:TemplateField>
     <ItemTemplate>
          <asp:LinkButton ID="btnDeleteInfoRequest" runat="server" onClick="DeleteInfoRequest(this, <%#DataBinder.Eval(Container.DataItem, "pKey") %>)" Text="Delete?" />
     </ItemTemplate>
</asp:TemplateField>

Upvotes: 0

Views: 1090

Answers (1)

Gregor Primar
Gregor Primar

Reputation: 6805

Change your aspx code like this:

        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="btnDeleteInfoRequest" runat="server" CommandArgument='<%#Eval("pKey")%>' OnClick="DeleteInfoRequest" Text="Delete?" />
            </ItemTemplate>
        </asp:TemplateField>

And create code behind like this:

Protected Sub DeleteInfoRequest(sender As Object, e As EventArgs)
    Dim btnDeleteInfoRequest As LinkButton = TryCast(sender, LinkButton)
    Dim pKey As String = btnDeleteInfoRequest.CommandArgument

    'TODO: do your stuff here
End Sub

Happy coding!

Upvotes: 1

Related Questions