Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Command event doesnt fire for button dynamic added to end of datagrid.

Status
Not open for further replies.

Jacqui1811

Programmer
Jul 8, 2002
100
SG
Hi,
I dynamically add a link button to my datagrid with a command name etc but the Item Command event handler won't fire.

I add the datagrid event handlers in the onload event like so :
grdFunds.EditCommand += new DataGridCommandEventHandler(grdFunds_Edit);
grdFunds.ItemCommand += new DataGridCommandEventHandler(grdFunds_Command);
grdFunds.DeleteCommand += new DataGridCommandEventHandler(grdFunds_Delete);
grdFunds.ItemDataBound += new DataGridItemEventHandler(grdFunds_ItemDataBound);
grdFunds.PreRender += new EventHandler(grdFunds_PreRender);

Then on the ItemDataBound event of the grid I do this and create a link button and add it to the empty column.
Like so :

protected void _view_grdFunds_ItemDataBound(Object sender, DataGridItemEventArgs e)
{
// get a reference to the empty last cell in the table.
TableCell myCell = e.Item.Cells[e.Item.Cells.Count - 1];

LinkButton lb = new LinkButton();

lb.Text = "Request Access to Extended Data";
lb.CausesValidation = false;
lb.EnableViewState = false;
lb.CommandName = "grdFunds_RequestExtendedAccess";
myCell.Controls.Add(lb);
}

I could definitely do with some help on this one folks, or even if you can think of a better way to add the button column to the end of the grid after the autogenerated rows, they have to be autogenerated as the number and content of rows can change, the button though has to be the last column. [bigears]

Jacqui.
 
You need to wire up the OnClick event.
lb.OnClick += myButton_Clicked;

Lodlaiden

You've got questions and source code. We want both!
There's a whole lot of Irish in that one.
 
Hi Lodlaiden
Thanks for your reply.

I added that but no luck.
I gave it the name of the function that the command event would have fired but no luck.
I have tried everything I have come across while searching, with and without a check for onpostback, with and without EnableViewState.
The buttons I add before the auto-generated columns work okay, it is just this one button which has to be at the end.
If I set a hyperlink there instead and redirect to another page that works so I am thinking of doing that and sending it to a page that will perform the action on loading if I don't get to the bottom of it soon, not tidy but will work I think.

Jacqui.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top