Groups | Blog | Home
all groups > dotnet academic > october 2005 >

dotnet academic : How to Find control in Header Template of Datagrid



Ajoy
10/12/2005 10:25:03 AM
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
10/19/2005 2:33:05 AM
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";
}

[quoted text, click to view]
Han
10/30/2005 2:01:03 AM
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


[quoted text, click to view]
AddThis Social Bookmark Button