收到错误....' RptFeedback_ItemID_0'类型' TextBox'必须放在带有runat = server的表单标记内

时间:2017-06-30 18:06:12

标签: c# asp.net webforms

我已经回顾了与此类似的其他问题,但尚未找到解决方案。我确定我只是遗漏了一些东西,但我确实有runat = server形式,所以不确定为什么会抛出这个错误。我还将所有文本框设置为runat" server"。我正在尝试使用一个按钮,在填充表单后将数据导出到excel。这是用c#落后的asp:

ASPX:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <br />
        <br />
        <br />
        <br />
        <asp:Label ID="Label1" runat="server" Text="Enter Portion ID"></asp:Label>
         &nbsp;&nbsp;&nbsp;<asp:TextBox ID="portionIDTextBox" runat="server"></asp:TextBox>
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="btnSearchFeedback" runat="server" Text="Search" OnClick="btnSearchFeedback_Click" />
        <br />
        <br />

         <asp:Repeater ID="RptFeedback" runat="server">
                        <HeaderTemplate>
                            <table>
                                <tr>
                                    <th>Item ID
                                    </th>
                                    <th>Item DB Key
                                    </th>
                                    <th>Candidate ID
                                    </th>    
                                    <th>Date
                                    </th>   
                                    <th>Feedback
                                    </th>                                                                    
                                </tr>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <tr>
                                <td>
                                   <asp:TextBox ID="ItemID"  runat="server" Width ="100px" Text='<%#Eval("Item ID") %>' ></asp:TextBox>
                                </td>
                                <td>
                                   <asp:TextBox ID="ItemDBKey"  runat="server" Width ="90px" Text='<%#Eval("Item DB Key") %>' ></asp:TextBox>
                                </td>
                                <td>
                                    <asp:TextBox ID="CandidateID" runat="server" Width ="100px" Text='<%#Eval("Candidate ID") %>'></asp:TextBox>
                                </td>    
                                <td>
                                    <asp:TextBox ID="Date" runat="server" Text='<%#Eval("Date") %>'></asp:TextBox>
                                </td>  
                                <td>
                                    <asp:TextBox ID="Feedback" runat="server" Width ="600px" Text='<%#Eval("Feedback") %>'></asp:TextBox>
                                </td>                        
                               </tr>
                        </ItemTemplate>
                        <FooterTemplate>
                            </table>
                        </FooterTemplate>                    
                    </asp:Repeater>
            <br />
        <asp:Button ID="btnExport" runat="server" Text="Export" OnClick = "ExportToExcel" />
    </div>
    </form>
</body>
</html>

cs for export btn:

 protected void ExportToExcel(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", 
        "attachment;filename=RepeaterExport.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        RptFeedback.RenderControl(hw);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }

和错误:

Server Error in '/' Application.
--------------------------------------------------------------------------------


Control 'RptFeedback_ItemID_0' of type 'TextBox' must be placed inside a form tag with runat=server. 
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

 Exception Details: System.Web.HttpException: Control 'RptFeedback_ItemID_0' of type 'TextBox' must be placed inside a form tag with runat=server.

Source Error: 



Line 40:         StringWriter sw = new StringWriter();
Line 41:         HtmlTextWriter hw = new HtmlTextWriter(sw);
Line 42:         RptFeedback.RenderControl(hw);
Line 43:         Response.Output.Write(sw.ToString());
Line 44:         Response.Flush(); 

1 个答案:

答案 0 :(得分:1)

您正在调用RptFeedback.RenderControl(hw),这引发了一个异常,即Server-Control是在Form之外呈现的。

您可以通过覆盖

来避免这种情况
public override void VerifyRenderingInServerForm(Control control)
{
   /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
 server control at run time. */
}