Attaching events to templated controls

A

AC

I've created a user control (ASCX) that contains two buttons. This control is programatically placed into a datagrid via the LoadTemplate method and the TemplateColumn object. What I'd like to do is attach events to the two buttons from the code that loaded the object... like so (any ideas... I know FindControl won't work... just don't/can't create the events from within the sort column):

SortColumn.ascx
<asp:button id="SortUp" runat="server" text="up" CommandName="SortUp"></asp:button>
<asp:button id="SortDown" runat="server" text="down" CommandName="SortDown"></asp:button>

ProgramaticDataGrid.aspx.cs
ITemplate sortColumnTemplate = Page.LoadTemplate("~/Controls/Templates/SortColumn.ascx");

// below doesn't work, but you see where i'm going
Button bUp = (Button)sortColumn.FindControl("SortUp");
bUp.Click += //event handler code
// above doesn't work, but you see where i'm going

TemplateColumn tc = new TemplateColumn();
tc.HeaderText = "Sorting";
tc.ItemTemplate = sortColumnTemplate;
DataGrid1.Columns.Add(tc);
// binding code next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top