How can I get to the EditCommandColumn?

  • Thread starter Thread starter needin4mation
  • Start date Start date
N

needin4mation

I have this in my DataGrid's ItemCreated event:

TableCell myChangeCell = e.Item.Cells[0];
LinkButton myChangeButton = (LinkButton) myTableCell.Controls[0];
myChangeButton.Text="Hello!";

I am trying to get to the button that is automatically created by the
control. I get an object reference error. I couldn't find an answer.
Thanks for any help.
 
I have this in my DataGrid's ItemCreated event:

TableCell myChangeCell = e.Item.Cells[0];
LinkButton myChangeButton = (LinkButton) myTableCell.Controls[0];
myChangeButton.Text="Hello!";

I am trying to get to the button that is automatically created by the
control. I get an object reference error. I couldn't find an answer.
Thanks for any help.

Have you wrapped your code in a conditional block:

if(e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType=ListItemType.AlternatingItem) {
//your code here
}

If not, your code will also be executed for the Header Template
and the Footer Template, which don't have any button, and this
will trigger the object reference error.

Riki
 
It's actually in this:

if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem ||
e.Item.ItemType == ListItemType.EditItem)
{

I don't know what the answer is.
 
The answer is I had the wrong reference myTableCell should have been
myChangeCell.

Also, to make it work with the PushButton, I had to change from
LinkButton to just Button.
 
Try using...

LinkButton button = (LinkButton) e.Item.FindControl("someLInkButton").

In item template in HTML make sure control has an ID and runat=server

Charlie
 
I can't use FindControl because it is auto generated; thus, the reason
for asking.
 
Then if is there any reason you want to make it AUTO GENERATE?.
Try using Templates and then FinControl
or use BoundColumns
Hope that helps
Patrick
 
Okay, maybe I don't understand how the grid works. I put the
EditCommandColumn in there. There grid is not set to autogenerate.
The button is because it is the EditCommandColumn. Are you saying that
I can get a button that will change to Update and Cancel that is not
automatic?
 
Back
Top