Fint a asp control on page but returns NULL
,HI,
On my .aspx page i have a dataGrid with id = "dataGrid1", I need to edit that control for on class in the app_code.
This is what i am doing in my class:
if (HttpContext.Current.Handler is Page)
{
Pag开发者_开发百科e currentPage = (Page)HttpContext.Current.Handler;
if (currentPage != null)
{
Control ctrl = FindControlRecursive(currentPage, "dataGrid1");
}
}
Then i have this recursive function:
public static Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}
return null;
}
But for some reason it doesnt find my control and returns null.
Can any one help???
Thank you
Since you're already getting instance of the page, try adding public method that will return the Grid, then have such code:
Page currentPage = (Page)HttpContext.Current.Handler;
if (currentPage != null)
{
DataGrid myGrid = (currentPage as YourClassName).GetGrid();
...
}
Also, what is the context of your code? When does it get executed?
精彩评论