一个页面只能有一个服务器端的表单标签错误。

3

按要求的Aspx页面

<%@ Page Title="" Language="C#" MasterPageFile="~/tier6a-educ.master" AutoEventWireup="true"
    EnableEventValidation="false" CodeFile="TAP_rubform_response.aspx.cs" Inherits="TAP_rubform_response" %>

<asp:Content ID="Content1" ContentPlaceHolderID="test1" runat="server">
    <head>
        <link href="StyleSheet.css" type="text/css" rel="Stylesheet" />
    </head>
    <div style="padding-left: 10pt">
        <form id="form1" runat="server">


        <asp:Table ID="Table11" runat="server" Height="16px" Width="845px">
            <asp:TableRow ID="TableRow10" runat="server" orderColor="Gray" BorderWidth="1pt"
                BorderStyle="Dotted" HorizontalAlign="Center">
                <asp:TableCell ID="TableCell10" runat="server" ColumnSpan="2"><h4>SECTION 1: STANDARDS AND OBJECTIVES</h4></asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <asp:Table ID="table38" runat="server" HorizontalAlign="center" BorderColor="Gray"
            BorderWidth="1pt" BorderStyle="Dotted" Width="850px" Style="line-height: 15pt"
            Font-Names="Trebuchet MS" Font-Size="11pt">
            <asp:TableRow ID="TableRow11" runat="server">
                <asp:TableCell ID="TableCell11" runat="server">
                    <asp:Label ID="Label22" runat="server">Rating &nbsp;       </asp:Label>
                    <asp:Label ID="Label23" Font-Bold="true" runat="server">    </asp:Label>
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow ID="TableRow34" runat="server">
                <asp:TableCell ID="TableCell44" runat="server">
                    <asp:Label ID="Label48" runat="server">Evidence &nbsp;       </asp:Label>
                    <asp:Label ID="Label49" Font-Bold="true" runat="server">   </asp:Label>
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <asp:Table ID="Table12" runat="server" Height="16px" Width="845px">
            <asp:TableRow ID="TableRow12" runat="server" orderColor="Gray" BorderWidth="1pt"
                BorderStyle="Dotted" HorizontalAlign="Center">
                <asp:TableCell ID="TableCell12" runat="server" ColumnSpan="2"><h4>SECTION 2: Presenting Instructional Content</h4></asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <asp:Table ID="table2" runat="server" HorizontalAlign="center" BorderColor="Gray"
            BorderWidth="1pt" BorderStyle="Dotted" Width="850px" Style="line-height: 15pt"
            Font-Names="Trebuchet MS" Font-Size="11pt">
            <asp:TableRow ID="TableRow13" runat="server">
                <asp:TableCell ID="TableCell15" runat="server">
                    <asp:Label ID="Label20" runat="server">Rating &nbsp;       </asp:Label>
                    <asp:Label ID="Label21" Font-Bold="true" runat="server">    </asp:Label>
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow ID="TableRow39" runat="server">
                <asp:TableCell ID="TableCell49" runat="server">
                    <asp:Label ID="Label58" runat="server">Evidence &nbsp;       </asp:Label>
                    <asp:Label ID="Label59" Font-Bold="true" runat="server">   </asp:Label>
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <asp:Table ID="Table13" runat="server" Height="16px" Width="845px">
            <asp:TableRow ID="TableRow14" runat="server" orderColor="Gray" BorderWidth="1pt"
                BorderStyle="Dotted" HorizontalAlign="Center">
                <asp:TableCell ID="TableCell13" runat="server" ColumnSpan="2"><h4>SECTION 3: Lesson Structure and Pacing</h4></asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <asp:Table ID="table3" runat="server" HorizontalAlign="center" BorderColor="Gray"
            BorderWidth="1pt" BorderStyle="Dotted" Width="850px" Style="line-height: 15pt"
            Font-Names="Trebuchet MS" Font-Size="11pt">
            <asp:TableRow ID="TableRow15" runat="server">
                <asp:TableCell ID="TableCell18" runat="server">
                    <asp:Label ID="Label24" runat="server">Rating &nbsp;       </asp:Label>
                    <asp:Label ID="Label25" Font-Bold="true" runat="server">    </asp:Label>
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow ID="TableRow35" runat="server">
                <asp:TableCell ID="TableCell45" runat="server">
                    <asp:Label ID="Label50" runat="server">Evidence &nbsp;       </asp:Label>
                    <asp:Label ID="Label51" Font-Bold="true" runat="server">   </asp:Label>
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <asp:Table ID="Table14" runat="server" Height="16px" Width="845px">
            <asp:TableRow ID="TableRow16" runat="server" orderColor="Gray" BorderWidth="1pt"
                BorderStyle="Dotted" HorizontalAlign="Center">
                <asp:TableCell ID="TableCell14" runat="server" ColumnSpan="2"><h4>SECTION 4: Activities and Materials </h4></asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <asp:Table ID="table4" runat="server" HorizontalAlign="center" BorderColor="Gray"
            BorderWidth="1pt" BorderStyle="Dotted" Width="850px" Style="line-height: 15pt"
            Font-Names="Trebuchet MS" Font-Size="11pt">
            <asp:TableRow ID="TableRow17" runat="server">
                <asp:TableCell ID="TableCell21" runat="server">
                    <asp:Label ID="Label26" runat="server">Rating &nbsp;       </asp:Label>
                    <asp:Label ID="Label27" Font-Bold="true" runat="server">    </asp:Label>
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow ID="TableRow36" runat="server">
                <asp:TableCell ID="TableCell46" runat="server">
                    <asp:Label ID="Label52" runat="server">Evidence &nbsp;       </asp:Label>
                    <asp:Label ID="Label53" Font-Bold="true" runat="server">   </asp:Label>
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <asp:Table ID="Table21" runat="server" Height="16px" Width="845px">
            <asp:TableRow ID="TableRow18" runat="server" orderColor="Gray" BorderWidth="1pt"
                BorderStyle="Dotted" HorizontalAlign="Center">
                <asp:TableCell ID="TableCell16" runat="server" ColumnSpan="2"><h4>SECTION 5: Academic Feedback</h4></asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <asp:Table ID="table5" runat="server" HorizontalAlign="center" BorderColor="Gray"
            BorderWidth="1pt" BorderStyle="Dotted" Width="850px" Style="line-height: 15pt"
            Font-Names="Trebuchet MS" Font-Size="11pt">
            <asp:TableRow ID="TableRow19" runat="server">
                <asp:TableCell ID="TableCell24" runat="server">
                    <asp:Label ID="Label28" runat="server">Rating &nbsp;       </asp:Label>
                    <asp:Label ID="Label29" Font-Bold="true" runat="server">    </asp:Label>
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow ID="TableRow37" runat="server">
                <asp:TableCell ID="TableCell47" runat="server">
                    <asp:Label ID="Label54" runat="server">Evidence &nbsp;       </asp:Label>
                    <asp:Label ID="Label55" Font-Bold="true" runat="server">   </asp:Label>
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <asp:Table ID="Table26" runat="server" Height="16px" Width="845px">
            <asp:TableRow ID="TableRow20" runat="server" orderColor="Gray" BorderWidth="1pt"
                BorderStyle="Dotted" HorizontalAlign="Center">
                <asp:TableCell ID="TableCell59" runat="server" ColumnSpan="2"><h4>SECTION 6: Grouping Students</h4></asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <asp:Table ID="table6" runat="server" HorizontalAlign="center" BorderColor="Gray"
            BorderWidth="1pt" BorderStyle="Dotted" Width="850px" Style="line-height: 15pt"
            Font-Names="Trebuchet MS" Font-Size="11pt">
            <asp:TableRow ID="TableRow21" runat="server">
                <asp:TableCell ID="TableCell27" runat="server">
                    <asp:Label ID="Label30" runat="server">Rating &nbsp;       </asp:Label>
                    <asp:Label ID="Label31" Font-Bold="true" runat="server">    </asp:Label>
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow ID="TableRow38" runat="server">
                <asp:TableCell ID="TableCell48" runat="server">
                    <asp:Label ID="Label56" runat="server">Evidence &nbsp;       </asp:Label>
                    <asp:Label ID="Label57" Font-Bold="true" runat="server">   </asp:Label>
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <asp:Table ID="Table32" runat="server" Height="16px" Width="845px">
            <asp:TableRow ID="TableRow40" runat="server" orderColor="Gray" BorderWidth="1pt"
                BorderStyle="Dotted" HorizontalAlign="Center">
                <asp:TableCell ID="TableCell71" runat="server" ColumnSpan="2"><h4>SECTION 7: Teacher Content Knowledge</h4></asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <asp:Table ID="table7" runat="server" HorizontalAlign="center" BorderColor="Gray"
            BorderWidth="1pt" BorderStyle="Dotted" Width="850px" Style="line-height: 15pt"
            Font-Names="Trebuchet MS" Font-Size="11pt">
            <asp:TableRow ID="TableRow23" runat="server">
                <asp:TableCell ID="TableCell30" runat="server">
                    <asp:Label ID="Label32" runat="server">Rating &nbsp;       </asp:Label>
                    <asp:Label ID="Label33" Font-Bold="true" runat="server">   </asp:Label>
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow ID="TableRow24" runat="server">
                <asp:TableCell ID="TableCell31" runat="server">
                    <asp:Label ID="Label34" runat="server">Evidence &nbsp;       </asp:Label>
                    <asp:Label ID="Label35" Font-Bold="true" runat="server">   </asp:Label>
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <asp:Table ID="Table39" runat="server" Height="16px" Width="845px">
            <asp:TableRow ID="TableRow42" runat="server" orderColor="Gray" BorderWidth="1pt"
                BorderStyle="Dotted" HorizontalAlign="Center">
                <asp:TableCell ID="TableCell91" runat="server" ColumnSpan="2"><h4>SECTION 8: Questioning</h4></asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <asp:Table ID="table8" runat="server" HorizontalAlign="center" BorderColor="Gray"
            BorderWidth="1pt" BorderStyle="Dotted" Width="850px" Style="line-height: 15pt"
            Font-Names="Trebuchet MS" Font-Size="11pt">
            <asp:TableRow ID="TableRow26" runat="server">
                <asp:TableCell ID="TableCell34" runat="server">
                    <asp:Label ID="Label36" runat="server">Rating &nbsp;       </asp:Label>
                    <asp:Label ID="Label37" Font-Bold="true" runat="server">   </asp:Label>
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow ID="TableRow27" runat="server">
                <asp:TableCell ID="TableCell35" runat="server">
                    <asp:Label ID="Label38" runat="server">Evidence &nbsp;       </asp:Label>
                    <asp:Label ID="Label39" Font-Bold="true" runat="server">   </asp:Label>
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <asp:Table ID="Table45" runat="server" Height="16px" Width="845px">
            <asp:TableRow ID="TableRow50" runat="server" orderColor="Gray" BorderWidth="1pt"
                BorderStyle="Dotted" HorizontalAlign="Center">
                <asp:TableCell ID="TableCell105" runat="server" ColumnSpan="2"><h4>SECTION 9: Thinking</h4></asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <asp:Table ID="table9" runat="server" HorizontalAlign="center" BorderColor="Gray"
            BorderWidth="1pt" BorderStyle="Dotted" Width="850px" Style="line-height: 15pt"
            Font-Names="Trebuchet MS" Font-Size="11pt">
            <asp:TableRow ID="TableRow29" runat="server">
                <asp:TableCell ID="TableCell38" runat="server">
                    <asp:Label ID="Label40" runat="server">Rating &nbsp;       </asp:Label>
                    <asp:Label ID="Label41" Font-Bold="true" runat="server">   </asp:Label>
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow ID="TableRow30" runat="server">
                <asp:TableCell ID="TableCell39" runat="server">
                    <asp:Label ID="Label42" runat="server">Evidence &nbsp;       </asp:Label>
                    <asp:Label ID="Label43" Font-Bold="true" runat="server">   </asp:Label>
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <asp:Table ID="Table50" runat="server" Height="16px" Width="845px">
            <asp:TableRow ID="TableRow56" runat="server" orderColor="Gray" BorderWidth="1pt"
                BorderStyle="Dotted" HorizontalAlign="Center">
                <asp:TableCell ID="TableCell117" runat="server" ColumnSpan="2"><h4>SECTION 10: Problem Solving</h4></asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <asp:Table ID="table10" runat="server" HorizontalAlign="center" BorderColor="Gray"
            BorderWidth="1pt" BorderStyle="Dotted" Width="850px" Style="line-height: 15pt"
            Font-Names="Trebuchet MS" Font-Size="11pt">
            <asp:TableRow ID="TableRow32" runat="server">
                <asp:TableCell ID="TableCell42" runat="server">
                    <asp:Label ID="Label44" runat="server">Rating &nbsp;       </asp:Label>
                    <asp:Label ID="Label45" Font-Bold="true" runat="server">   </asp:Label>
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow ID="TableRow33" runat="server">
                <asp:TableCell ID="TableCell43" runat="server">
                    <asp:Label ID="Label46" runat="server">Evidence &nbsp;       </asp:Label>
                    <asp:Label ID="Label47" Font-Bold="true" runat="server">   </asp:Label>
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <asp:Table ID="Table15" runat="server" BorderColor="Gray" BorderWidth="1pt" BorderStyle="Dotted"
            Width="850px" HorizontalAlign="Center">
            <asp:TableRow ID="TableRow48" runat="server">
                <asp:TableCell ID="TableCell101" HorizontalAlign="left" ColumnSpan="1" runat="server"
                    Font-Size="Large">
                    <asp:Button ID="Button1" Text="Send As e-Mail" runat="server" OnClick="SendAsEmail_Click"
                        Font-Size="10pt" Height="18pt" Width="90pt" ValidationGroup="onsubmit" />
                </asp:TableCell>
                <asp:TableCell ID="TableCell19" HorizontalAlign="center" ColumnSpan="1" runat="server"
                    Font-Size="Large">
                    <asp:Button ID="Button3" Text="Save As PDF" runat="server" OnClick="SaveAsPDF_Click"
                        Font-Size="10pt" Height="18pt" Width="90pt" ValidationGroup="onsubmit" />
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        </form>
    </div>
</asp:Content>

我有一个aspx页面,在页面中有一个按钮,当用户点击该按钮时,页面将以电子邮件的形式发送给用户。
这是我为按钮的click函数编写的代码。
protected void SendasEmail_Click(object sender, EventArgs e)
{
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    this.Page.RenderControl(hw);

    SmtpClient smtpClient = new SmtpClient();
    MailMessage message = new MailMessage();

    MailAddress fromAddress = new MailAddress("xxxxx");
    smtpClient.Host = "xxxxxxx";
    smtpClient.Port = 25;
    message.From = fromAddress;
    message.To.Add("xxxxxx");
    message.Subject = "The webpage is below";
    message.IsBodyHtml = true;
    message.Body = sw.ToString();        
    smtpClient.Send(message);
    }

我可以正常接收邮件,但执行页面后会显示以下错误。
我确定页面中只有一个表单,因为如果上述函数的实现为空,我就不会得到任何错误。网页加载正常,点击按钮没有任何反应。
一个页面只能有一个服务器端Form标签。
描述:当前Web请求执行期间发生未处理的异常。请查看堆栈跟踪以获取有关错误和代码中源的更多信息。
异常详细信息:System.Web.HttpException:一个页面只能有一个服务器端Form标记。
源代码错误:
在当前Web请求执行期间生成了一个未处理的异常。可以使用以下异常堆栈跟踪来识别异常的起源和位置。
堆栈跟踪:
[HttpException (0x80004005): A page can have only one server-side Form tag.]
   System.Web.UI.Page.OnFormRender() +8721176
   System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer) +33
   System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +32
   System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output) +51
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer) +40
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Control.Render(HtmlTextWriter writer) +10
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Control.Render(HtmlTextWriter writer) +10
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Page.Render(HtmlTextWriter writer) +29
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266

看起来这个错误是因为我调用了函数 this.Page.RenderControl(hw);

有没有人对如何解决这个问题有经验?

谢谢。


你为什么要调用 this.Page.RenderControl(hw); - mellamokb
为了将页面内容存储到变量hw中(并间接存储到变量sw中),以便将其复制到message.Body = sw.ToString()中。 - user1067334
你能否也发布你的.aspx代码? - Emmanuel N
@emmanuel-n:我已经发布了aspx代码。 - user1067334
2个回答

3
看起来你可能已经在页面上禁用了EventValidation,否则你会收到一个RegisterForEventValidation can only be called during Render(); 错误信息。
目前的情况似乎是整个页面都被渲染到了HtmlTextWriter中,并在Render事件期间重新渲染以在Response中输出。这可能会导致ASP看到两个Form标签并抛出该错误。解决方法是重写页面的Render方法,捕获您想要的HTML内容,将其发送到电子邮件并将HTML写入Response以在页面上显示。 此链接提供了有关正在发生的情况以及如何重写Render的良好说明。
编辑:你应该能够调用base.Render并捕获输出以写入Response和你的电子邮件。
在按钮单击事件中,设置一个布尔值指示是否向页面发送电子邮件:
public void myButton_Click(object sender, EventArgs e)
{
    okayToEmail = true;
}

当您覆盖渲染时,请将页面渲染到新的 HtmlTextWriter 中,将内容写入页面的 HtmlTextWriter,并检查标志的值。如果为 true,请通过电子邮件发送内容。

protected override void Render(HtmlTextWriter writer)
{
    var sb = new StringBuilder();
    using (var sw = new StringWriter(sb))
    {
        using (var htw = new HtmlTextWriter(sw))
        {
            base.Render(htw);
            writer.Write(sb.ToString());

            if (okayToEmail)
            {
                using (var message = new MailMessage())
                {
                    message.Body = sb.ToString();
                    ...
                }
            }
        }
    }
}

记住:将整个页面写入电子邮件正文中可能并不好…它会包括Doctype标签,htmlbody标签等,这些将被大多数(可能全部)电子邮件客户端删除。 此外,链接的样式表将不起作用,我相信大多数电子邮件客户端也会删除嵌入式样式表。


也许他应该创建一个新的 WebRequest 来获取所需的 HTML?或者使用 Server.Execute()。而不是覆盖 Render... 看起来这可能会产生一些负面影响;这不是只有 ASP.NET 主机才能调用的方法吗? - Bryan
我尝试了你发的链接中提供的解决方案,但是我仍然遇到了同样的错误。那个人使用的重写函数是这个:protected override void Render(HtmlTextWriter writer) { if (okToSendMarkup) SendRenderedMarkup();base.Render(writer);}所以,当我遇到错误时,它仍然调用了base.Render()。 - user1067334
请检查我的修改。我假设你的 SendRenderedMarkup 方法重新渲染了页面,这可能导致了你的问题。我的上述修改对我有效。 - Dave Zych

1

有一种标准的方法来拦截和处理发送到浏览器的响应。
如果您想接收并处理此响应,则应使用响应过滤器

private void Page_Load(object sender, EventArgs e)
{

  // Filter the text to be rendered as all uppercase.
  Response.Filter = new UpperCaseFilterStream(Response.Filter);
}

UpperCaseFilter类是一个自定义的流类,它将通过它的所有文本转换为大写。

Asp.net将调用流的Write方法。由于流可能会接收到分块数据,因此您需要在流类内部进行一些缓冲。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接