ASP.NET - Access final rendered HTML of FormView control
Asked Answered
A

2

2

I am trying to use a separate webpage to generate HTML that can be returned via AJAX to another webpage to update a panel. However, I cannot find a way to access the rendered HTML to return via Response.Write(...).

My webpage code is as follows:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WarehouseDetails.aspx.cs" Inherits="WarehouseDetails" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="frmMain" runat="server">
        <div id="content" runat="server">
        <asp:FormView ID="fvwWhseDetails" runat="server" AllowPaging="false" RenderOuterTable="false">
            <ItemTemplate>
                <div class="col-md-8"><asp:TextBox ID="txtItemNum" runat="server" CssClass="form-control" Text='<%# Eval("OrderCode") %>' disabled/></div>
                ....
                ....
                ....
            </ItemTemplate>
        </asp:FormView>
        </div>
   </form>
</body>
</html>

After the page is loaded, it renders HTML similar to the following:

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title></head>
<body>
    <form method="post" action="WarehouseDetails.aspx" id="frmMain">
        <div id="content">
            <div class="col-md-8"><input name="fvwWhseDetails$txtItemNum" type="text" value="159580" id="fvwWhseDetails_txtItemNum" class="form-control" disabled="" /></div>
            ....
            ....
            ....
        </div>
    </form>
</body>
</html>

What I am trying to accomplish is returning all of the rendered HTML contained withing the ... area. However, nothing I have tried seems to work.

In my code-behind file, I have tried the following methods for HTML retrieval with the respective errors commented below each method:

public partial class WarehouseDetails : System.Web.UI.Page
{
    protected string strORAConnectionString = ConfigurationManager.ConnectionStrings["OraConnectionString"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        string strSelect = @"...";

        using (OleDbConnection oraHQDB = new OleDbConnection(strORAConnectionString))
        {

            OleDbCommand command = new OleDbCommand(strSelect, oraHQDB);
            oraHQDB.Open();

            OleDbDataReader reader = command.ExecuteReader();
            if(reader.HasRows)
            {
                fvwWhseDetails.DataSource = reader;
                fvwWhseDetails.DataBind();

                // Everything up until this point works fine.
                // If the code below is excluded,
                // the page renders correctly.

                string resp;

                // Method 1 - try to access InnerHTML of <div id="content">...</div>
                System.Web.UI.HtmlControls.HtmlGenericControl div = (System.Web.UI.HtmlControls.HtmlGenericControl)frmMain.FindControl("content");
                string resp = div.InnerHtml;
                // Error generated:
                //    System.Web.HttpException:
                //    Cannot get inner content because the contents are not literal.

                // Method 2 - try to use RenderControl()
                StringWriter sw = new StringWriter();
                HtmlTextWriter w = new HtmlTextWriter(sw);
                frmMain.RenderControl(w);
                fvwWhseDetails.RenderControl(w);
                fvwWhseDetails.Row.RenderControl(w);                    
                div.RenderControl(w);
                resp = sw.GetStringBuilder().ToString();
                // Error generated: 
                //    System.Web.HttpException:
                //    Control '[control]' of type '[type]' must be placed inside a form tag with runat=server.

                Response.Write(resp);
            }
            else
            {
                Response.Write("No results found.");
            }
            Response.End();
            reader.Close();
        }
    }
}

I've searched repeatedly and tried wrapping the content in various containers and server controls, but regardless I always end up back at one of the two errors listed above.

What is the proper way to access the fully rendered HTML within a FormView?

Autonomic answered 27/6, 2014 at 16:49 Comment(0)
A
1

I found a simple solution to what I was looking for via this answer:

https://mcmap.net/q/568308/-registerforeventvalidation-can-only-be-called-during-render

Basically I had to override the VerifyRederingInServerForm method to bypass verification that the control was contained within a <form ... runat="server"> when invoking fvwWhseDetails.Rendercontrol(w);.

Additionally, I had to disable event validation via setting <%@ Page ............ EnableEventValidation="false" %> on the page to allow the FormView control to be rendered independently.

I don't fully understand the implications of bypassing both of these safeguards, but since this page is only used to return this single FormView control, I'm going to go forward with solution for now. If anyone has a more appropriate solution, please feel free to contribute and I will change my answer accordingly.

Autonomic answered 27/6, 2014 at 19:56 Comment(0)
B
0

If you wrap your FormView in a user control, you could define an HttpHandler (myFormView.ashx) which could load just that control, render, and respond with the generated HTML.

See http://madskristensen.net/post/load-user-controls-from-an-httphandler-in-aspnet to get yourself started.

Bein answered 27/6, 2014 at 17:6 Comment(1)
I will give this a shot and report back, but it seems like a lot of added complexity to simply extra the rendered HTML of a FormView control.Autonomic

© 2022 - 2024 — McMap. All rights reserved.