Link to home
Start Free TrialLog in
Avatar of SeTech
SeTechFlag for United States of America

asked on

Exporting GridView to Excel - usual stuff isn't working

I know that these days, the question of exporting a GridView to Excel is a tired, worn-out one. Questions and answers and websites aboutn on this topic, so don't think I'm just asking the same old how-do-I here. I've got a problem that's annoying, but likely has a simple solution, I just don't know what it is.

So the setup is simple. A GridView. An ImageButton that is supposed to export to Excel when clicked. It's not the only GridView-to-Excel setup I've worked with, and the others are functioning properly. My suspicion is that what's throwing this one off is the fact of having the Edit/Delete column. The last column in the GridView is an Edit/Delete column that is a TemplateColumn. And when I try to export to Excel, the error message I get says "Control 'ctl07' of type 'DataControlLinkButton' must be placed inside a form tag with runat=server.". And if I try to override the "VerifyRenderingInServerForm(Control control)" method, I get yet another error about not being able to call it when I'm trying to, blah blah blah. I even tryied to remove the last column before exporting (as you'll see inside the code snippet below), but it doesn't seem to be working.

In order to try to resolve earlier issues, I am implementing a method I got from the website http://mattberseth.com/, that basically takes the contents of a GridView, breaks it down row by row, cell by cell, and makes everything plain text inside an HTML table, and exports THAT to Excel. But when I try to throw some kind of catch in there for the "DataControlLinkButton" control, I can't because Intellisense can't figure it out and the compiler doesn't recognize it. I even added an else in this structure that if the control inside a cell is none of the recognized one, it's supposed to just flat-out remove it. But somehow, this is slipping through, and I am officially stumped.

So I am attaching the declaration of the GridView column below, so you can see what I mean there, and also the code for my Exporting to Excel methods, so if the answer is in there, glaringly obvious, please help me see it.

Thanks in advance for all help offered!
<asp:TemplateField HeaderText="Edit">
                    <ItemTemplate>
                        <asp:LinkButton runat="server" Text="Edit" CommandName="Edit" CausesValidation="false" />
                        <asp:LinkButton runat="server" Text="Delete" CommandName="Delete" CausesValidation="false" />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:LinkButton runat="server" Text="Update" CommandName="Update" CausesValidation="true" />
                        <asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="false" />
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:LinkButton ID="lnkAdd" runat="server" Text="Add" Width="80%" CommandName="Insert"
                            CausesValidation="true" ValidationGroup="OkToValidate" OnClick="lnkAdd_Click" />
                    </FooterTemplate>
                </asp:TemplateField>
///////////////////////////////////////////////////////
protected void btnExport_Click(object sender, ImageClickEventArgs e)
    {
        gvResult.AllowPaging = false;
        gvResult.ShowFooter = false;
        DataControlField dt;
        dt = gvResult.Columns[7];
        gvResult.Columns.Remove(gvResult.Columns[7]);
        Export("claimdir.xls", gvResult);
        gvResult.AllowPaging = true;
        gvResult.ShowFooter = true;
        gvResult.Columns.Insert(7, dt);
    }
 
    /// <summary>
    /// Got this online from http://mattberseth.com/
    /// </summary>
    /// <param name="fileName"></param>
    /// <param name="gv"></param>
    public static void Export(string fileName, GridView gv)
    {
        string style = @"<style> .mytext { mso-number-format:\@; } </style> ";
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader(
            "content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";
 
        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                //  Create a table to contain the grid
                Table table = new Table();
 
                //  include the gridline settings
                table.GridLines = gv.GridLines;
 
                //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                    PrepareControlForExport(gv.HeaderRow);
                    table.Rows.Add(gv.HeaderRow);
                }
 
                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    PrepareControlForExport(row);
                    table.Rows.Add(row);
                }
 
                //  add the footer row to the table -- not in this page
                //if (gv.FooterRow != null)
                //{
                //    PrepareControlForExport(gv.FooterRow);
                //    table.Rows.Add(gv.FooterRow);
                //}
 
                //  render the table into the htmlwriter
                table.RenderControl(htw);
 
                //  render the htmlwriter into the response
                HttpContext.Current.Response.Write(style);
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }
 
    /// <summary>
    /// Replace any of the contained controls with literals
    /// Got this online from http://mattberseth.com/
    /// </summary>
    /// <param name="control"></param>
    private static void PrepareControlForExport(Control control)
    {
        for (int i = 0; i < control.Controls.Count; i++)
        {
            Control current = control.Controls[i];
            if (current is LinkButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
            }
            else if (current is ImageButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
            }
            else if (current is HyperLink)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
            }
            else if (current is DropDownList)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
            }
            else if (current is CheckBox)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
            }
            else if (current is TextBox)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as TextBox).Text));
            }
            else
            {
                control.Controls.Remove(current);
            }
 
            if (current.HasControls())
            {
                PrepareControlForExport(current);
            }
        }
    }

Open in new window

Avatar of raterus
raterus
Flag of United States of America image

Based on this article, a "DataControlLinkButton" simply inherits from LinkButton, so your code should work.

http://forums.asp.net/p/1039667/1444869.aspx

Can you set trace="true" on the top of your aspx page, display the gridview, copy the results, try to export, and paste the error again.  I'd like to see where this control exists, because my hunch is your PrepareControlForExport() method isn't touching it.
Avatar of SeTech

ASKER

OK. I remember all the tracing you had me doing last time. This is the same page, BTW, it's just the last little battle I'm having to fight on it. If I can get the Excel piece working, I will FINALLY be done with it. Anyhow, here's the trace for just displaying the GridView :

aspx.page Begin PreInit  
aspx.page End PreInit 0.00166082560772389 0.001661
aspx.page Begin Init 0.00200472406409194 0.000344
aspx.page End Init 0.00277912416242847 0.000774
aspx.page Begin InitComplete 0.00300429244498952 0.000225
aspx.page End InitComplete 0.00312106706299264 0.000117
aspx.page Begin LoadState 0.00334316232929045 0.000222
aspx.page End LoadState 0.00427372752682254 0.000931
aspx.page Begin ProcessPostData 0.00436312436357135 0.000089
aspx.page End ProcessPostData 0.00483441331230645 0.000471
aspx.page Begin PreLoad 0.00492101649790686 0.000087
aspx.page End PreLoad 0.00515512446414279 0.000234
aspx.page Begin Load 0.00523921336370963 0.000084
aspx.page End Load 0.0330312930833388 0.027792
aspx.page Begin ProcessPostData Second Try 0.0333207153423131 0.000289
aspx.page End ProcessPostData Second Try 0.0334416804370388 0.000121
aspx.page Begin Raise ChangedEvents 0.0335235344156869 0.000082
aspx.page End Raise ChangedEvents 0.0341339471916123 0.000610
aspx.page Begin Raise PostBackEvent 0.0342336805376102 0.000100
aspx.page End Raise PostBackEvent 0.128454035359243 0.094220
aspx.page Begin LoadComplete 0.128617463951424 0.000163
aspx.page End LoadComplete 0.129110822744231 0.000493
aspx.page Begin PreRender 0.129196029104258 0.000085
aspx.page End PreRender 0.282016848510076 0.152821
aspx.page Begin PreRenderComplete 0.282419134275446 0.000402
aspx.page End PreRenderComplete 0.282883997826539 0.000465
aspx.page Begin SaveState 0.336552830038455 0.053669
aspx.page End SaveState 0.343153668971894 0.006601
aspx.page Begin SaveStateComplete 0.343258151524845 0.000104
aspx.page End SaveStateComplete 0.343340843598837 0.000083
aspx.page Begin Render 0.343428843610012 0.000088
aspx.page End Render 0.381790702449613 0.038362

And when I try to export, the error I get (with full stack trace) is :

System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.Web.HttpException: Control 'ctl07' of type 'DataControlLinkButton' must be placed inside a form tag with runat=server.
   at System.Web.UI.Page.VerifyRenderingInServerForm(Control control)
   at System.Web.UI.WebControls.LinkButton.AddAttributesToRender(HtmlTextWriter writer)
   at System.Web.UI.WebControls.WebControl.RenderBeginTag(HtmlTextWriter writer)
   at System.Web.UI.WebControls.DataControlLinkButton.Render(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   at System.Web.UI.WebControls.TableCell.RenderContents(HtmlTextWriter writer)
   at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   at System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer)
   at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   at System.Web.UI.WebControls.Table.RenderContents(HtmlTextWriter writer)
   at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   at claimdir.Export(String fileName, GridView gv) in d:\Inetpub\wwwroot\MSG\claimdir.aspx.cs:line 286
   at claimdir.btnExport_Click(Object sender, ImageClickEventArgs e) in d:\Inetpub\wwwroot\MSG\claimdir.aspx.cs:line 235
   at System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e)
   at System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   --- End of inner exception stack trace ---
   at System.Web.UI.Page.HandleError(Exception e)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest()
   at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
   at System.Web.UI.Page.ProcessRequest(HttpContext context)
   at ASP.claimdir_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\msg\136ea4a7\3c2a5bc4\App_Web_ikrzhhkh.8.cs:line 0
   at System.Web.HttpApplication.CallHandlerExecutionStep.Sy

And just to be disgustingly thorough, the "claimdir.aspx.cs:line 286" it refers to and "line 235" that are referenced are :

235 : Export("claimdir.xls", gvResult);
286: table.RenderControl(htw);

Though I think the context for those lines are in the code snippet in my original post.
Thanks, I actually need the control tree which is in the trace somewhere.  Can you save the page as HTML and attach it, It'll be easier to decipher!
Avatar of SeTech

ASKER

Well, for security purposes, I don't think I can do the absolute whole page, but if you need the control tree, I'll save that as HTML and kill the rest of the page with sensitive info and attach that...it looks really jacked up because the formatting and images are gone, and I chopped out a lot of the sensitive info, but the Control Tree is there. Also, the file upload won't let me upload an HTML file (even a ZIPped one), so I tacked on a .txt. to the end. Just remove the .txt and it should be fine.
My-SERO-Gateway.htm.txt
Try this, just for kicks,  Side note, you don't have to undo your changes to the grid after you export since the ViewState isn't being changed.

protected void btnExport_Click(object sender, ImageClickEventArgs e)
    {
        gvResult.AllowPaging = false;
        gvResult.AllowSorting = false; // I think this is your problem, there are LinkButtons in your header
        gvResult.ShowFooter = false;
        DataControlField dt;
        dt = gvResult.Columns[7];
        gvResult.Columns.Remove(gvResult.Columns[7]);
    }
Avatar of SeTech

ASKER

OK, I tried that, but still to no avail, I got the exact same error message.
here's what I want you to do,  

1. keep trace="true" on, and keep AllowSorting=false in the Export routine
2. comment out these lines in the export routine (check code snippet)
3. Try to export the grid
3. Hopefully you'll see onscreen any "LinkButtons", note them in your response for clarity. Regardless of what you see, post the control tree here again from the trace.
                //  render the table into the htmlwriter
                table.RenderControl(htw);
 
                //  render the htmlwriter into the response
                HttpContext.Current.Response.Write(style);
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();

Open in new window

Avatar of SeTech

ASKER

Hrmmm, that turned out interestingly...

When I went to export this time, it did bring up Excel, but it basically exported the whole freaking page, complete with everything on it, to Excel. So I saved that Excel file, and will attach it, just in case. I also copied the Control Tree again, and saved it to a Word doc. Yes, it's too wide for Print view, but if you look at it in Normal view, you can see all of it.

I actually like the way the grid looks in this export (except being partially covered by a random gray box), as it actually has color, as opposed to the very stark looking black-and-white export I normally get using the other method. I dunno if it's possible to preserve that at this point or not, but just thought I'd note it.

Anyway, the files are attached. Hope they can help shed light.
claimdir-1-.xls
Control-Tree.doc
SOLUTION
Avatar of raterus
raterus
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SeTech

ASKER

Hmmm, wonder if that almost worked. I got an error on it, but the error I got was :

System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.InvalidOperationException: RegisterForEventValidation can only be called during Render();
   at System.Web.UI.ClientScriptManager.RegisterForEventValidation(String uniqueId, String argument)
   at System.Web.UI.ClientScriptManager.GetPostBackEventReference(PostBackOptions options, Boolean registerForEventValidation)
   at System.Web.UI.WebControls.LinkButton.AddAttributesToRender(HtmlTextWriter writer)
   at System.Web.UI.WebControls.WebControl.RenderBeginTag(HtmlTextWriter writer)
   at System.Web.UI.WebControls.DataControlLinkButton.Render(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   at System.Web.UI.WebControls.TableCell.RenderContents(HtmlTextWriter writer)
   at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   at System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer)
   at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   at System.Web.UI.WebControls.Table.RenderContents(HtmlTextWriter writer)
   at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   at System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer)
   at System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output)
   at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer)
   at claimdir.Export(String fileName, GridView gv) in d:\Inetpub\wwwroot\MSG\claimdir.aspx.cs:line 285
   at claimdir.btnExport_Click(Object sender, ImageClickEventArgs e) in d:\Inetpub\wwwroot\MSG\claimdir.aspx.cs:line 234
   at System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e)
   at System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   --- End of inner exception stack trace ---

I'm not explicitly calling that method anywhere, though.
I guess you kinda want the Header records.  Just for kicks, if you set this (below) at the start of your Export routine, does it work?.  I know your problem is in the header, and I'm really surprised that AllowSorting=false didn't work for you in the first place.

gvResult.ShowHeader = false;

Forget the last suggestion I made with the HTMLForm and what not, and back up to where I had you comment out some fields.  Keep those commented, and also comment out these lines (below) and get me the control tree, and the HTML source of the gridview would help too!


        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader(
            "content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

Open in new window

Avatar of SeTech

ASKER

OK, after adding the ShowHeader = false, and commenting out all the lines you specified, clicking the Export button gets me the page re-displayed as usual, but with the grid having no header, no footer, and no Edit/Delete column. Basically, everything I want in the export except the header.

Makes me wonder if I could somehow fudge that by creating another header row with only the text from the header...probably too much trouble, nevermind...

Anyhow, the control tree is attached, and the HTML source is here in the code snippet. I assumed you wanted the HTML source of the pre-export grid, but just to be safe, I'm putting both. Better overkill than having to re-post.
Before "Export" :
        <div>
		<table class="salesdirgrid" cellspacing="0" cellpadding="2" rules="all" border="1" id="ctl00_MSG_Content_gvResult" style="width:100%;border-collapse:collapse;">
			<tr style="color:White;background-color:#1C66AF;font-weight:bold;">
				<td><a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult','Sort$DeskLoc')" style="color:White;">Desk Location</a></td><td><a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult','Sort$FName')" style="color:White;">First Name</a></td><td><a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult','Sort$LName')" style="color:White;">Last Name</a></td><td><a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult','Sort$Phone')" style="color:White;">Phone</a></td><td><a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult','Sort$Office')" style="color:White;">Office</a></td><td>Edit</td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl02_lblDeskLoc">VSP</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl02_lblFName">Steven</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl02_lblLName">Polk</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl02_lblPhone">999-999-9999</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl02_lblOffice">ATLANTA AUTO MCO</span>
                    </td><td>
                        <a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult$ctl02$ctl00','')">Edit</a>
                        <a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult$ctl02$ctl01','')">Delete</a>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl03_lblDeskLoc">HCP</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl03_lblFName">Christopher</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl03_lblLName">Pope</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl03_lblPhone">678-742-3741</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl03_lblOffice">ATLANTA METRO MCO</span>
                    </td><td>
                        <a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult$ctl03$ctl00','')">Edit</a>
                        <a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult$ctl03$ctl01','')">Delete</a>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl04_lblDeskLoc">WGD</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl04_lblFName">Greg</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl04_lblLName">Davenport</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl04_lblPhone">800-366-3264</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl04_lblOffice">ALABAMA MCO</span>
                    </td><td>
                        <a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult$ctl04$ctl00','')">Edit</a>
                        <a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult$ctl04$ctl01','')">Delete</a>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl05_lblDeskLoc">VUM</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl05_lblFName">Kevin</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl05_lblLName">Poythress</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl05_lblPhone">919-594-0443</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl05_lblOffice">COASTAL CAROLINA</span>
                    </td><td>
                        <a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult$ctl05$ctl00','')">Edit</a>
                        <a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult$ctl05$ctl01','')">Delete</a>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl06_lblDeskLoc">BAT</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl06_lblFName">Ando</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl06_lblLName">Poore</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl06_lblPhone">678-589-5554</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl06_lblOffice">MPC</span>
                    </td><td>
                        <a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult$ctl06$ctl00','')">Edit</a>
                        <a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult$ctl06$ctl01','')">Delete</a>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl07_lblDeskLoc">FIP</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl07_lblFName">Leslie</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl07_lblLName">Porter</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl07_lblPhone">678-742-4159</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl07_lblOffice">SUBROGATION</span>
                    </td><td>
                        <a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult$ctl07$ctl00','')">Edit</a>
                        <a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult$ctl07$ctl01','')">Delete</a>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl08_lblDeskLoc">2OS</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl08_lblFName">Loretta</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl08_lblLName">Posey</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl08_lblPhone">866-575-4363</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl08_lblOffice">MPC</span>
                    </td><td>
                        <a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult$ctl08$ctl00','')">Edit</a>
                        <a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult$ctl08$ctl01','')">Delete</a>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl09_lblDeskLoc">2O2</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl09_lblFName">Monica</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl09_lblLName">Powell</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl09_lblPhone">866-575-4363</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl09_lblOffice">MPC</span>
                    </td><td>
                        <a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult$ctl09$ctl00','')">Edit</a>
                        <a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult$ctl09$ctl01','')">Delete</a>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl10_lblDeskLoc">SSA</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl10_lblFName">Sheta</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl10_lblLName">Powell</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl10_lblPhone">866-575-4363</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl10_lblOffice">MPC</span>
                    </td><td>
                        <a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult$ctl10$ctl00','')">Edit</a>
                        <a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult$ctl10$ctl01','')">Delete</a>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl11_lblDeskLoc">YBR</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl11_lblFName">Robert</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl11_lblLName">Posey</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl11_lblPhone">866-538-4773</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl11_lblOffice">AUTO EXPRESS-BIRMINGHAM</span>
                    </td><td>
                        <a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult$ctl11$ctl00','')">Edit</a>
                        <a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult$ctl11$ctl01','')">Delete</a>
                    </td>
			</tr><tr style="color:#4A3C8C;background-color:#1C66AF;">
				<td>
                        <input name="ctl00$MSG_Content$gvResult$ctl12$txtNewDeskLoc" type="text" maxlength="3" id="ctl00_MSG_Content_gvResult_ctl12_txtNewDeskLoc" style="width:100%;" />
                        <span id="ctl00_MSG_Content_gvResult_ctl12_ReqNewDeskLoc" style="color:Red;visibility:hidden;">*</span>
                         
                    </td><td>
                        <input name="ctl00$MSG_Content$gvResult$ctl12$txtNewFName" type="text" maxlength="20" id="ctl00_MSG_Content_gvResult_ctl12_txtNewFName" style="width:100%;" />
                        <span id="ctl00_MSG_Content_gvResult_ctl12_ReqNewFName" style="color:Red;visibility:hidden;">*</span>
                        
                    </td><td>
                        <input name="ctl00$MSG_Content$gvResult$ctl12$txtNewLName" type="text" maxlength="20" id="ctl00_MSG_Content_gvResult_ctl12_txtNewLName" style="width:100%;" />
                        <span id="ctl00_MSG_Content_gvResult_ctl12_ReqNewLName" style="color:Red;visibility:hidden;">*</span>
                        
                    </td><td>
                        <input name="ctl00$MSG_Content$gvResult$ctl12$txtNewPhone" type="text" maxlength="20" id="ctl00_MSG_Content_gvResult_ctl12_txtNewPhone" style="width:100%;" />
                        <span id="ctl00_MSG_Content_gvResult_ctl12_ReqNewPhone" style="color:Red;visibility:hidden;">*</span>
                        
                    </td><td>
                        <select name="ctl00$MSG_Content$gvResult$ctl12$ddlNewOfficeUpdate" id="ctl00_MSG_Content_gvResult_ctl12_ddlNewOfficeUpdate" style="font-size:XX-Small;">
					<option value="%">-Choose Office-</option>
					<option value="ALABAMA MCO">ALABAMA MCO</option>
					<option value="ATLANTA AUTO MCO">ATLANTA AUTO MCO</option>
					<option value="ATLANTA METRO MCO">ATLANTA METRO MCO</option>
					<option value="ATLANTA SIU">ATLANTA SIU</option>
					<option value="AUTO EXPRESS-BIRMINGHAM">AUTO EXPRESS-BIRMINGHAM</option>
					<option value="BIRMINGHAM SIU">BIRMINGHAM SIU</option>
					<option value="CHARLESTON">CHARLESTON</option>
					<option value="CHARLOTTE SIU">CHARLOTTE SIU</option>
					<option value="COASTAL CAROLINA">COASTAL CAROLINA</option>
					<option value="MACON MCO">MACON MCO</option>
					<option value="MACON SIU">MACON SIU</option>
					<option value="MPC">MPC</option>
					<option value="PALMETTO">PALMETTO</option>
					<option value="PALMETTO STU">PALMETTO SIU</option>
					<option value="PIEDMONT">PIEDMONT</option>
					<option value="RALEIGH SIU">RALEIGH SIU</option>
					<option value="SE SHORES CSA">SE SHORES CSA</option>
					<option value="SOUTHEAST PROPERTY">SOUTHEAST PROPERTY</option>
					<option value="SUBROGATION">SUBROGATION</option>
 
				</select>
                    </td><td>
                        <a id="ctl00_MSG_Content_gvResult_ctl12_lnkAdd" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$MSG_Content$gvResult$ctl12$lnkAdd&quot;, &quot;&quot;, true, &quot;OkToValidate&quot;, &quot;&quot;, false, true))" style="display:inline-block;width:80%;">Add</a>
                    </td>
			</tr><tr align="left" style="color:White;background-color:#1C66AF;">
				<td colspan="6"><table border="0">
					<tr>
						<td><span>1</span></td><td><a href="javascript:__doPostBack('ctl00$MSG_Content$gvResult','Page$2')" style="color:White;">2</a></td>
					</tr>
				</table></td>
			</tr>
		</table>
	</div>
----------------------------------------
After "Export":
 
        <div>
		<table class="salesdirgrid" cellspacing="0" cellpadding="2" rules="all" border="1" id="ctl00_MSG_Content_gvResult" style="width:100%;border-collapse:collapse;">
			<tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl02_lblDeskLoc">VSP</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl02_lblFName">Steven</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl02_lblLName">Polk</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl02_lblPhone">999-999-9999</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl02_lblOffice">ATLANTA AUTO MCO</span>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl03_lblDeskLoc">HCP</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl03_lblFName">Christopher</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl03_lblLName">Pope</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl03_lblPhone">678-742-3741</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl03_lblOffice">ATLANTA METRO MCO</span>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl04_lblDeskLoc">WGD</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl04_lblFName">Greg</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl04_lblLName">Davenport</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl04_lblPhone">800-366-3264</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl04_lblOffice">ALABAMA MCO</span>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl05_lblDeskLoc">VUM</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl05_lblFName">Kevin</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl05_lblLName">Poythress</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl05_lblPhone">919-594-0443</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl05_lblOffice">COASTAL CAROLINA</span>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl06_lblDeskLoc">BAT</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl06_lblFName">Ando</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl06_lblLName">Poore</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl06_lblPhone">678-589-5554</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl06_lblOffice">MPC</span>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl07_lblDeskLoc">FIP</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl07_lblFName">Leslie</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl07_lblLName">Porter</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl07_lblPhone">678-742-4159</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl07_lblOffice">SUBROGATION</span>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl08_lblDeskLoc">2OS</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl08_lblFName">Loretta</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl08_lblLName">Posey</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl08_lblPhone">866-575-4363</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl08_lblOffice">MPC</span>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl09_lblDeskLoc">2O2</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl09_lblFName">Monica</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl09_lblLName">Powell</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl09_lblPhone">866-575-4363</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl09_lblOffice">MPC</span>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl10_lblDeskLoc">SSA</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl10_lblFName">Sheta</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl10_lblLName">Powell</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl10_lblPhone">866-575-4363</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl10_lblOffice">MPC</span>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl11_lblDeskLoc">YBR</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl11_lblFName">Robert</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl11_lblLName">Posey</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl11_lblPhone">866-538-4773</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl11_lblOffice">AUTO EXPRESS-BIRMINGHAM</span>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl12_lblDeskLoc">RRP</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl12_lblFName">Ros</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl12_lblLName">Porterfield</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl12_lblPhone">843-202-3479</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl12_lblOffice">CHARLESTON</span>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl13_lblDeskLoc">GTP</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl13_lblFName">Tanya</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl13_lblLName">Porter</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl13_lblPhone">919-954-7495</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl13_lblOffice">COASTAL CAROLINA</span>
                    </td>
			</tr>
		</table>
	</div>

Open in new window

Control-Tree.doc
Let me confirm something really quickly.  I am only interested in the control tree/HTML source AFTER hou hit the export control!  Is this last Control-Tree before?

Also, don't keep ShowHeader=false when you run these, I just wanted to see if that fixed your problem, and it did...sorta!  Looks like you had it on when you ran the example, so ...sigh... I'll have to ask you to rerun it, and just show me stuff AFTER!

We'll figure this out eventually!
Avatar of SeTech

ASKER

*smacks forehead* Ah well, I guess it could be worse.

Yes, the Control Tree I posted there was after I hit Export, but I guess that doesn't matter much at this point, as I'll be re-posting...
        <div>
		<table class="salesdirgrid" cellspacing="0" cellpadding="2" rules="all" border="1" id="ctl00_MSG_Content_gvResult" style="width:100%;border-collapse:collapse;">
			<tr style="color:White;background-color:#1C66AF;font-weight:bold;">
				<td>Desk Location</td><td>First Name</td><td>Last Name</td><td>Phone</td><td>Office</td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl02_lblDeskLoc">VSP</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl02_lblFName">Steven</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl02_lblLName">Polk</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl02_lblPhone">999-999-9999</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl02_lblOffice">ATLANTA AUTO MCO</span>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl03_lblDeskLoc">HCP</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl03_lblFName">Christopher</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl03_lblLName">Pope</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl03_lblPhone">678-742-3741</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl03_lblOffice">ATLANTA METRO MCO</span>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl04_lblDeskLoc">WGD</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl04_lblFName">Greg</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl04_lblLName">Davenport</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl04_lblPhone">800-366-3264</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl04_lblOffice">ALABAMA MCO</span>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl05_lblDeskLoc">VUM</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl05_lblFName">Kevin</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl05_lblLName">Poythress</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl05_lblPhone">919-594-0443</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl05_lblOffice">COASTAL CAROLINA</span>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl06_lblDeskLoc">BAT</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl06_lblFName">Ando</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl06_lblLName">Poore</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl06_lblPhone">678-589-5554</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl06_lblOffice">MPC</span>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl07_lblDeskLoc">FIP</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl07_lblFName">Leslie</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl07_lblLName">Porter</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl07_lblPhone">678-742-4159</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl07_lblOffice">SUBROGATION</span>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl08_lblDeskLoc">2OS</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl08_lblFName">Loretta</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl08_lblLName">Posey</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl08_lblPhone">866-575-4363</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl08_lblOffice">MPC</span>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl09_lblDeskLoc">2O2</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl09_lblFName">Monica</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl09_lblLName">Powell</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl09_lblPhone">866-575-4363</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl09_lblOffice">MPC</span>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl10_lblDeskLoc">SSA</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl10_lblFName">Sheta</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl10_lblLName">Powell</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl10_lblPhone">866-575-4363</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl10_lblOffice">MPC</span>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl11_lblDeskLoc">YBR</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl11_lblFName">Robert</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl11_lblLName">Posey</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl11_lblPhone">866-538-4773</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl11_lblOffice">AUTO EXPRESS-BIRMINGHAM</span>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl12_lblDeskLoc">RRP</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl12_lblFName">Ros</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl12_lblLName">Porterfield</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl12_lblPhone">843-202-3479</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl12_lblOffice">CHARLESTON</span>
                    </td>
			</tr><tr>
				<td style="width:10%;">
                        <span id="ctl00_MSG_Content_gvResult_ctl13_lblDeskLoc">GTP</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl13_lblFName">Tanya</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl13_lblLName">Porter</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl13_lblPhone">919-954-7495</span>
                    </td><td>
                        <span id="ctl00_MSG_Content_gvResult_ctl13_lblOffice">COASTAL CAROLINA</span>
                    </td>
			</tr>
		</table>
	</div>

Open in new window

Control-Tree.doc
Avatar of SeTech

ASKER

OK, well, I am about to leave for the day, but if you get anything put up here, I'll see it first thing Monday morning, and we'll work more on it. Thanks for helping me get as far as we have!
Looks like the DataControlLinkButtons are not in the control tree anymore, so I'd be interested to see your error now if you put back the code the way it was.  I do see some Textbox's and Validators in the last row of the gridview, almost like you have a footer row that is acting like an insert?

I hope you're not so busy giving me control trees and HTML output that you're forgetting to actually test if any of this is working! :-)
Avatar of SeTech

ASKER

Well, I did just un-comment the code I previously had commented, and it blew up again, so something's still wrong. The assumption you made about the Footer row is 100% correct, that's why you might note in my Exporting routine, I commented out the second about the FooterRow, as I didn't want it added to the table for that very reason.

I really don't understand at this point what's making it blow up like it is, it's still squawking about control 'ctl07' of type DataControlLinkButton. Somehow it's just not pulling the text and removing the control.
The "gv.Rows" isn't just datarows, but it will include the header and footer rows as well.  If you want to ensure your export is just a header and datarows, try this (below).  Comment out your "HeaderRow" check code as well.

foreach (GridViewRow row in gv.Rows)
{
    if ((row.RowType==DataControlRowType.Header) || (row.RowType==DataControlRowType.DataRow))
    {
        PrepareControlForExport(row);
        table.Rows.Add(row);
    }
}
Avatar of SeTech

ASKER

Nope, that still throws an error, but it's at least a different error this time...slightly...

It's still about something being placed inside a form tag blah blah blah, but this time it's just a plan LinkButton, not a DataControlLinkButton, and it's a different control, it's 'ctl10', which pretty much still just confuses me.
Can you post all your updated code at this point?

Also, is you rewrote you code like this, does it work? (Don't keep it like this, just see if it works)

    if (row.RowType==DataControlRowType.DataRow)
    {
        PrepareControlForExport(row);
        table.Rows.Add(row);
    }

Avatar of SeTech

ASKER

Nope, even with that, I still get an error, same as the other I posted today. At this point, my code looks like this (snippet below).
protected void btnExport_Click(object sender, ImageClickEventArgs e)
    {
        gvResult.AllowPaging = false;
        gvResult.AllowSorting = false;
        gvResult.ShowFooter = false;
        gvResult.Columns.Remove(gvResult.Columns[7]);
        Export("claimdir.xls", gvResult);
    }
 
    /// <summary>
    /// Got this online from http://mattberseth.com/ - adjustments by Ando and "raterus" from www.experts-exchange.com
    /// </summary>
    /// <param name="fileName"></param>
    /// <param name="gv"></param>
    private void Export(string fileName, GridView gv)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader(
            "content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";
 
        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                //  Create a table to contain the grid
                Table table = new Table();
 
                //  include the gridline settings
                table.GridLines = gv.GridLines;
 
                //  add the header row to the table
                //if (gv.HeaderRow != null)
                //{
                //    PrepareControlForExport(gv.HeaderRow);
                //    table.Rows.Add(gv.HeaderRow);
                //}
 
                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        PrepareControlForExport(row);
                        table.Rows.Add(row);
                    }
                }
 
                //  add the footer row to the table -- not in this page (apoab)
                //if (gv.FooterRow != null)
                //{
                //    PrepareControlForExport(gv.FooterRow);
                //    table.Rows.Add(gv.FooterRow);
                //}
                
                //  render the table into the htmlwriter
                table.RenderControl(htw);
 
                //  render the htmlwriter into the response
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }
 
    /// <summary>
    /// Replace any of the contained controls with literals
    /// Got this online from http://mattberseth.com/
    /// </summary>
    /// <param name="control"></param>
    private void PrepareControlForExport(Control control)
    {
        for (int i = 0; i < control.Controls.Count; i++)
        {
            Control current = control.Controls[i];
            if (current is LinkButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
            }
            else if (current is ImageButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
            }
            else if (current is HyperLink)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
            }
            else if (current is DropDownList)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
            }
            else if (current is CheckBox)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
            }
            else if (current is TextBox)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as TextBox).Text));
            }
            else
            {
                control.Controls.Remove(current);
            }
 
            if (current.HasControls())
            {
                PrepareControlForExport(current);
            }
        }
    }

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SeTech

ASKER

Hmmm, well, there's progress...

That stopped the errors, but now all that exports to Excel is the hidden ID field, the First Name field, and the Phone Number field. None of the others exported. Still, better than nothing, right?
That's probably your export routine at this point, it needs to be tweaked to find and export all possible text.  Post the complete source of your GridView.

Basically what was happening was you were turning off Paging,Sorting, Footer, etc, but you were never rebinding the grid to update it with these changes.
Avatar of SeTech

ASKER

Well, that definitely is a plus, and here's another plus. While I was playing around with it on my own, trying desperately to figure out something, anything, I went back to the earlier thing with the HtmlForm object, and instead of "this.Page", I went with just a new Page (I'll post the snippet below), and it's working now, exporting all the rows, and now even is prettier with colors instead of B&W. I guess the main problem was the DataBind. Sure enough, a simple solution that I just didn't know about.

Thanks so much for your help (again)!
HtmlForm f = new HtmlForm();
                f.Page = new Page();
                f.Controls.Add(gv);
                f.RenderControl(htw);
 
                //  render the htmlwriter into the response
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();

Open in new window

Avatar of SeTech

ASKER

Second time in a row you've been the one to really help me. Kudos on being a freaking genius.
Glad you got it working.