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?