ASP.NET - 访问最终渲染FormView控件的HTML [英] ASP.NET - Access final rendered HTML of FormView control

查看:115
本文介绍了ASP.NET - 访问最终渲染FormView控件的HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用一个单独的网页生成可以通过AJAX返回到另一个网页更新面板的HTML。不过,我不能找到一种方法来访问所提供的HTML通过的Response.Write(...)返回。

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(...).

我的网页上code是如下:

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>

在页面加载

之后,它呈现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>

我试图做到的是返回所有呈现的HTML包含withing的...区域。然而,没有什么我都试过似乎工作。

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.

在我的code-隐藏文件,我已经试过了HTML检索与评价每种方法如下相应错误下面的方法:

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.

什么是正确的方法来一个FormView中访问完全呈现的HTML?

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

推荐答案

我发现了一个简单的解决方案是什么,我通过这个答案在寻找:

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

http://stackoverflow.com/a/17058409/363892

基本上,我不得不重写 VerifyRederingInServerForm 方法,控制被包含在&LT内绕过验证,形式... =服务器&GT; 调用时 fvwWhseDetails.Rendercontrol(W);

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);.

此外,我不得不通过设定&LT禁用事件验证;%@页............ EnableEventValidation =假%&GT; 在页面上,使FormView控件可以独立呈现。

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

我不完全了解绕过这两个保障措施的影响,但由于该页面只是用来返回这个单一FormView控件,我要前进与解决方案现在。如果任何人有一个更合适的解决方案,请随时奉献,我会相应地改变我的答案。

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.

这篇关于ASP.NET - 访问最终渲染FormView控件的HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆