asp.netdetailsview

ASP.NET DetailsView EmptyDataTemplate button handler


I put a button in the EmptyDataTemplate of a DetailsView. If I double-click it it will set a handler for me in the code-behind.

Is there a way to setup the handler without having to double-click the button in the designer.

In the code-behind I don't have access to the button id for me to attach a handler to it?

So I manually put OnClick="btnNew_Click" attribute in the markup. Is there a way to do this in the code-behind?

<asp:DetailsView ID="DetailsView2" runat="server" AutoGenerateRows="False" DataKeyNames="Id" DataSourceID="ObjectDataSource3" Height="50px" Width="50%">
    <EmptyDataTemplate>
        <asp:LinkButton ID="lnkNewNote" OnClick="btnNew_Click" runat="server" CausesValidation="True" CommandName="New" Text="New"></asp:LinkButton>
    </EmptyDataTemplate>

Solution

  • You can use FindContol to locate the LinkButton in the EmptyDataTemplate and add a Click event.

    LinkButton lb = DetailsView2.FindControl("lnkNewNote") as LinkButton;
    lb.Click += btnNew_Click;