Discussion:
How to Find control in Header Template of Datagrid
(too old to reply)
Ajoy
20 years ago
Permalink
Hi,

I have a label control which resides in the header template of a template
column in a datagrid. I have to reference this label control from the event
handler of a Datalist control.
DataList_ItemCommand is the function in which I have to reference the label
control of datagrid.
How do I find it. Datagrid.FindControl does not find this control because it
is in header template of datagrid.

Please advise

Thank you
Ajoy
Altunbay
20 years ago
Permalink
Hi, You can access it at the itemdatabound event of datagrid. First item to
bound is header item.
if(if(e.Item.ItemType == ListItemType.Header))
{
((Label)e.Item.Cells[0].Controls[1]).Text ="bla bla";
}
Post by Ajoy
Hi,
I have a label control which resides in the header template of a template
column in a datagrid. I have to reference this label control from the event
handler of a Datalist control.
DataList_ItemCommand is the function in which I have to reference the label
control of datagrid.
How do I find it. Datagrid.FindControl does not find this control because it
is in header template of datagrid.
Please advise
Thank you
Ajoy
Han
20 years ago
Permalink
Hi,

Don't use Datagrid variable declared in the code behind file.
for example: Datagrid1.FindControl
Asp.net page may not assign the datagrid object which post back from client,
after it's initialized in the Initialize component method.

But the datagrid object from the client is really existed in the form
object, because client postback everything in the form tag. So you can still
have it.

And please remember there is control hierarchy, and your object which
generate event is also in the hierarchy which is hierarchy of page object.
Then, you consider to use like the following in the event handler:

private void DataList_ItemCommand(Object sender, DataGridCommandEventArgs e)
{
DataGrid dtg = (DataGrid)sender.Parent.Parent.Parent;
Label lbl = (Label)dtg.FindControl("lblID");
}

In the event handler,
->sender is the source object which raise event.
->sender.Parent might be a cell of the datagrid that hold the control which
raise event.
->sender.Parent.Parent might be a row of the datagrid that hold the cell.
->sender.Parent.Parent.Parent might be datagrid that hold the row.

Now, you get the existing datagrid in the page and you can find your control.

Enjoy it!
--
Regards,
Kyaw Zayar Han
Post by Ajoy
Hi,
I have a label control which resides in the header template of a template
column in a datagrid. I have to reference this label control from the event
handler of a Datalist control.
DataList_ItemCommand is the function in which I have to reference the label
control of datagrid.
How do I find it. Datagrid.FindControl does not find this control because it
is in header template of datagrid.
Please advise
Thank you
Ajoy
Continue reading on narkive:
Search results for 'How to Find control in Header Template of Datagrid' (Questions and Answers)
5
replies
can i get question answer of asp.net ?
started 19 years ago
software
Loading...