我想使用一個單獨的網頁來生成HTML,可以通過AJAX返回到另一個網頁來更新面板。但是,我無法找到訪問呈現的HTML以通過Response.Write(...)返回的方法。ASP.NET - 訪問FormView控件的最終呈現HTML
我的網頁代碼如下:頁面加載
<%@ 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>
後,它呈現HTML類似以下內容:
<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>
我試圖做到的是返回所有的呈現HTML包含...區域。然而,我沒有嘗試過似乎工作。
在我的代碼隱藏文件,我已經嘗試了HTML檢索與評價每種方法低於各自的錯誤以下方法:
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();
}
}
}
我已經多次搜查,並試圖在包裝各種容器的內容和服務器控件,但不管我總是最終返回上面列出的兩個錯誤之一。
在FormView中訪問完整呈現的HTML的正確方法是什麼?
我會給出一個鏡頭並報告回來,但它看起來像增加了一些額外的複雜性,只需額外提供一個FormView控件的HTML。 –