6

How do I connect a button to a function in C#, ASP? Maybe something like:

    <TemplateField>
        <ItemTemplate>
             <asp:Button ID="btnFunction1" runat="server CommandName="Function1"/>
        </ItemTemplate>
    </TemplateField>

C# Class:

    protected void Function1()
    {
    }

1 Answer 1

3

That depends on what you want to do...if you want to process a GridView command use the OnRowCommand event instead...

<asp:GridView ID="grid" runat="server" OnRowCommand="grid_RowCommand">
<Columns>
   <TemplateField>
        <ItemTemplate>
             <asp:Button ID="btnFunction1" runat="server CommandName="Delete"/>
        </ItemTemplate>
    </TemplateField>
</Columns>
</asp:GridView>

Then on code-behind....

protected void grid_RowCommand(Object sender, GridViewCommandEventArgs e)
{
}

But if you want to simply handle the button's click event then, register the event declaratively using the Click event...

<TemplateField>
    <ItemTemplate>
         <asp:Button ID="btnFunction1" runat="server OnClick="Function1"/>
    </ItemTemplate>
</TemplateField>

and adjust your event handler method to match the same method signature of the Click event...

protected void Function1(object sender, EventArgs args)
{
}
Sign up to request clarification or add additional context in comments.

1 Comment

OnClick and its handlers are what I just need. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.