Here's an extract from a GridView exporter I wrote that converts controls in a GridView into literals that are re-formated. It might be of some help:
/// <summary>
/// Parses and cleans up data from the GridView controls collection
/// to make the data more suitable for Exported
/// </summary>
/// <param name="gv">The GridView to parse</param>
private void CleanUpControls(Control gv)
{
Literal l = new Literal();
for (int i = 0; i < gv.Controls.Count; i++)
{
if (gv.Controls[i].GetType() == typeof (LinkButton))
{
l.Text = (gv.Controls[i] as LinkButton).Text;
ReplaceWithLiteral(gv, l, i);
}
else if (gv.Controls[i].GetType() == typeof (ListControl))
{
l.Text = (gv.Controls[i] as ListControl).SelectedItem.Text;
ReplaceWithLiteral(gv, l, i);
}
else if (gv.Controls[i].GetType() == typeof (CheckBox))
{
l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
ReplaceWithLiteral(gv, l, i);
}
else if (gv.Controls[i].GetType() == typeof (BooleanImage))
{
l.Text = (gv.Controls[i] as BooleanImage).Value ? "True" : "False";
ReplaceWithLiteral(gv, l, i);
}
else if (gv.Controls[i].GetType().ToString() == "System.Web.UI.WebControls.PagerTable")
ReplaceWithLiteral(gv, l, i);
else if (gv.Controls[i].GetType() == typeof (HyperLink))
{
HyperLink hl = gv.Controls[i] as HyperLink;
if (MakeHyperLinksAbsolute)
{
if (hl != null)
hl.NavigateUrl = UrlHelper.MakeAbsoluteUrl(hl.NavigateUrl);
}
switch (TreatHyperLinksAs)
{
case HyperLinkMode.Text:
l.Text = hl.Text;
ReplaceWithLiteral(gv, l, i);
break;
case HyperLinkMode.NavigateUrl:
if (hl != null) l.Text = hl.NavigateUrl;
ReplaceWithLiteral(gv, l, i);
break;
case HyperLinkMode.ToolTip:
l.Text = hl.ToolTip;
ReplaceWithLiteral(gv, l, i);
break;
case HyperLinkMode.TextAndLink:
l.Text = String.Format("{0} ({1})", hl.Text, hl.NavigateUrl);
ReplaceWithLiteral(gv, l, i);
break;
case HyperLinkMode.HyperLink:
break;
}
}
if (gv.Controls[i].HasControls())
CleanUpControls(gv.Controls[i]);
}
}