How can I get to the EditCommandColumn?

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.
 
R

Riki

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
 
N

needin4mation

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.
 
N

needin4mation

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.
 
C

Charlie@NISH

Try using...

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

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

Charlie
 
N

needin4mation

I can't use FindControl because it is auto generated; thus, the reason
for asking.
 
P

Patrick Olurotimi Ige

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
 
N

needin4mation

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?
 

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

Top