Guzzyman
Guzzyman

Reputation: 561

Cannot get inner HTML

I ran into the error: Cannot get inner content of ExportDiv because the contents are not literal I did a search and got this very useful resource!

I know I used server controls in my aspx page. The page fetches data dynamically from the database. How can I export this data to excel given that there're server controls in my html page.

Here're some of the site.aspx code to the page

    <form id="form1" runat="server">
    <div runat="server" id="ExportDiv">
        <asp:Panel ID="ResultsPanel" runat="server">
        <table cellpadding="0" class="style1">
            <tr>
                <td class="style2">
                    <asp:DetailsView ID="DetailsView1" runat="server" CellPadding="4" 
                    </asp:DetailsView>
                </td>
                <td>
                    Poor</td>
                <td>
                    Good</td>
                <td class="style3">
                    Very Good</td>
                <td>
                    Total Responses</td>
                <td>
                    Average Score</td>
            </tr>
            <tr>
                <td class="style2" colspan="6" bgcolor="#CCCCCC">
                    1. How would you rate the food served to you?</td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp; &nbsp;a.) Overall Quality Taste and Flavour.<br />
                    &nbsp; &nbsp;b.) Variety of Food.</td>
                <td>
                    <asp:Label ID="lblResult0" runat="server" Text="Label"></asp:Label><br/>
                    <asp:Label ID="lblResult3" runat="server" Text="Label"></asp:Label>
                </td>
                <td>
                    <asp:Label ID="lblResult1" runat="server" Text="Label"></asp:Label><br/>
                    <asp:Label ID="lblResult4" runat="server" Text="Label"></asp:Label>
                </td>
                <td class="style3">
                    <asp:Label ID="lblResult2" runat="server" Text="Label"></asp:Label><br/>
                    <asp:Label ID="lblResult5" runat="server" Text="Label"></asp:Label>
                </td>
                <td>
                    <asp:Label ID="aveNum0" runat="server" Text="Label"></asp:Label>
                    <br/>
                    <asp:Label ID="aveNum1" runat="server" Text="Label"></asp:Label>
                    <br/>
                </td>
                <td>
                    <asp:Label ID="aveScore0" runat="server" Text="Label" style="font-weight: 700"></asp:Label>
                    <br />
                    <asp:Label ID="aveScore1" runat="server" Text="Label" style="font-weight: 700"></asp:Label>
                    <br/>
                </td>
            </tr>
           </table>
        </asp:Panel>


    </div>
                    <asp:GridView ID="gvSurveyResult" runat="server">
    </asp:GridView>
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    </form>
</body>

And here's also the site.aspx.cs code

    Response.AppendHeader("content-disposition", "attachment;filename=ExportedHtml.xls");
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "application/vnd.ms-excel";
    this.EnableViewState = false;
    Response.Write(ExportDiv.InnerHtml);
    Response.End();

All answers would be welcomed!

Upvotes: 1

Views: 1994

Answers (2)

Shashank Chaturvedi
Shashank Chaturvedi

Reputation: 2793

I would suggest you to create a function that will convert your data into comma separated values and then render the same as CSV instead of excel format. Both CSV and excel format files would open the same way in excel.

Upvotes: 1

SethMW
SethMW

Reputation: 1082

It's been a while since I've done Server Controls, etc., but will this do the trick?

http://harouny.com/2012/10/15/render-asp-net-controls-user-controls-to-html-by-code/

Upvotes: 1

Related Questions