Asp.NET Composite Control with GridView, RowCommand doesn't work

Z

Zule

Hi,

I am creating a Composite Control within which I have a GridView. In
this GridView, I have three TemplateFields, the last one being a
template with a LinkButton which is meant to be clicked so I can
capture this event to do something about it. The problem is that in the
CreateChildControls method, I initialize the GridView and link its
RowCommand event to a method (so I can capture the LinkButton Click)
but it doesn't work... I put a breakpoint in the debugger in the
RowCommand handler method, but it's never called when I click the
LinkButton. Here's the code:

protected override void CreateChildControls()
{
Controls.Clear();

templateFactory = CreateTemplateFactory();
// This templateFactory is an abstractFactory I made so I can
initialize some
// default templates for the GridView programmatically

gvNextWorkItemsAndParticipants = new GridView();
gvNextWorkItemsAndParticipants.ID =
"gvNextWorkItemsAndParticipants";
gvNextWorkItemsAndParticipants.AutoGenerateColumns = false;

InitializeGridViewColumns();

gvNextWorkItemsAndParticipants.RowDataBound +=
new GridViewRowEventHandler(
gvNextTasksAndParticipants_RowDataBound );
gvNextWorkItemsAndParticipants.RowCreated +=
new GridViewRowEventHandler(
gvNextWorkItemsAndParticipants_RowCreated );
gvNextWorkItemsAndParticipants.RowCommand +=
new GridViewCommandEventHandler(
gvNextWorkItemsAndParticipants_RowCommand );
}

private void gvNextTasksAndParticipants_RowDataBound( object sender,
GridViewRowEventArgs e )
{
if ( e.Row.RowType == DataControlRowType.DataRow )
{
//WorkItemBase, Task etc. are some business objects of mine
//I won't detail it here to get things simple
WorkItemBase workItem = ( WorkItemBase )e.Row.DataItem;

Label lblTask = ( Label )e.Row.FindControl( "lblTask" );
lblTask.Text = workItem.Name;

if ( !( workItem is GoldenTrack.General.WorkItems.Task ) )
{
return;
}

GoldenTrack.General.WorkItems.Task task = ( General.WorkItems.Task
)workItem;
Repeater rptParticipants = ( Repeater )e.Row.FindControl(
"rptParticipants" );
rptParticipants.ItemDataBound += new RepeaterItemEventHandler(
rptParticipants_ItemDataBound );
string membersString = task.Participants.ToString();
rptParticipants.DataSource =
GetGoldenAccessDataSource().DataSource.BulkLoad( membersString
);
rptParticipants.DataBind();

LinkButton lbtEditParticipants = ( LinkButton )e.Row.FindControl(
"lbtEditParticipants" );
bool canChooseParticipants = CanChooseParticipants( task );
lbtEditParticipants.Visible = canChooseParticipants;
lbtEditParticipants.CommandName = "EditParticipants";
lbtEditParticipants.CommandArgument = "" + task.Id;
}
}

private void gvNextWorkItemsAndParticipants_RowCreated( object sender,
GridViewRowEventArgs e )
{
if ( e.Row.RowType == DataControlRowType.DataRow )
{
LinkButton lbtEditParticipants = ( LinkButton )
e.Row.FindControl( "lbtEditParticipants" );
lbtEditParticipants.ToolTip = EditParticipantsToolTip;
}
}

void gvNextWorkItemsAndParticipants_RowCommand( object sender,
GridViewCommandEventArgs e )
{
// Do Something
}

private void InitializeGridViewColumns()
{
TemplateField tasksColumn = new TemplateField();
tasksColumn.HeaderText = NextWorkItemsHeaderText;
tasksColumn.ItemTemplate = templateFactory.GetTasksChoiceTemplate();
gvNextWorkItemsAndParticipants.Columns.Add( tasksColumn );

TemplateField participantsColumn = new TemplateField();
participantsColumn.HeaderText = ParticipantsHeaderText;
participantsColumn.ItemTemplate =
templateFactory.GetParticipantListTemplate();
gvNextWorkItemsAndParticipants.Columns.Add( participantsColumn );

TemplateField chooseParticipantsColumn = new TemplateField();
chooseParticipantsColumn.ItemTemplate =
templateFactory.GetEditParticipantLinkTemplate();
gvNextWorkItemsAndParticipants.Columns.Add( chooseParticipantsColumn
);

Controls.Add( gvNextWorkItemsAndParticipants );
}

Does anyone of you guys know what may be going on?

Thanks,
Zule
 
Z

Zule

I forgot that the event handler bindings should have been made in the
OnInit method. I did it like this:

protected override void OnInit( EventArgs e )
{
EnsureChildControls();
base.OnInit( e );
InitializeComponent();
}

private void InitializeComponent()
{
gvWorkItems.RowCommand +=
new GridViewCommandEventHandler(
gvNextWorkItemsAndParticipants_RowCommand );
gvWorkItems.RowDataBound +=
new GridViewRowEventHandler( gvWorkItems_RowDataBound );
gvWorkItems.RowCreated +=
new GridViewRowEventHandler(
gvNextWorkItemsAndParticipants_RowCreated );
}

And it worked great.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top