2017-06-30 22 views
1

我已經回顧了與此類似的其他問題,但尚未找到解決方案。我確信我只是缺少一些東西,但我確實有runat = server的形式,所以不知道爲什麼它會拋出這個錯誤。我也有所有的文本框設置runat「服務器」。我試圖有一個按鈕,該窗體填充後導出數據爲Excel。這是ASP與後面的C#:獲取錯誤....'TextBox'類型的'RptFeedback_ItemID_0'必須放置在runat = server的表格標籤內

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出口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(); 
+0

開始避免這種情況,如[EPPlus(http://stackoverflow.com/documentation/epplus/drafts/98280)爲例。你現在正在做的是創建一個擴展名爲.xls的HTML頁面。正如你所看到的,你的技術會產生更多的問題。 – VDWWD

回答

1

要調用RptFeedback.RenderControl( hw),這引發了一個例外,即服務器控件在一個窗體之外被渲染。

您可以通過使用專門的庫創建Excel文件覆蓋

public override void VerifyRenderingInServerForm(Control control) 
{ 
    /* Confirms that an HtmlForm control is rendered for the specified ASP.NET 
server control at run time. */ 
} 
+0

我必須添加EnableEventValidation =「false」才能生成excel文檔。 Text不在Excel單元格內,但它確實以某種奇怪的方式將它們放在那裏。謝謝。 –

相關問題