2012-10-24 41 views
0

我的代碼有什麼問題?視覺工作室沒有得到任何警告或錯誤。錯誤呈現控件 - WebUserControl,發生未處理的異常

ASP.NET,錯誤呈現控件 - WebUserControl,發生未處理的異常。從Web瀏覽器

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs"   Inherits="WebUserControl" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<script runat="server"> 

    protected void UploadButton_Click(object sender, EventArgs e) 
    { 
     // Specify the path on the server to 
     // save the uploaded file to. 
     String savePath = @"c:\temp\uploads\"; 

     // Before attempting to perform operations 
     // on the file, verify that the FileUpload 
     // control contains a file. 
     if (FileUpload1.HasFile) 
     { 
      // Get the name of the file to upload. 
      String fileName = FileUpload1.FileName; 

      // Append the name of the file to upload to the path. 
      savePath += fileName; 


      // Call the SaveAs method to save the 
      // uploaded file to the specified path. 
      // This example does not perform all 
      // the necessary error checking.    
      // If a file with the same name 
      // already exists in the specified path, 
      // the uploaded file overwrites it. 
      FileUpload1.SaveAs(savePath); 

      // Notify the user of the name of the file 
      // was saved under. 
      UploadStatusLabel.Text = "Your file was saved as " + fileName; 
     } 
     else 
     { 
      // Notify the user that a file was not uploaded. 
      UploadStatusLabel.Text = "You did not specify a file to upload."; 
     } 

    } 

</script> 

<html > 
<head id="Head1" runat="server"> 
    <title>FileUpload Example</title> 
    <style type="text/css"> 
     .style1 
     { 
      width: 100%; 
     } 
     .style2 
     { 
      height: 26px; 
     } 
    </style> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <table class="style1"> 
     <tr> 
      <td> 
       Select a file to upload:</td> 
      <td> 
       &nbsp;</td> 
     </tr> 
     <tr> 
      <td class="style2"> 

     <asp:FileUpload id="FileUpload1"     
      runat="server"> 
     </asp:FileUpload> 

      </td> 
      <td class="style2"> 
      </td> 
     </tr> 
     <tr> 
      <td> 

     <asp:Button id="UploadButton" 
      Text="Upload file" 
      OnClick="UploadButton_Click" 
      runat="server"> 
     </asp:Button>  

      </td> 
      <td> 
       &nbsp;</td> 
     </tr> 
     <tr> 
      <td> 

     <hr /> 

      </td> 
      <td> 
       &nbsp;</td> 
     </tr> 
     <tr> 
      <td> 

     <asp:Label id="UploadStatusLabel" 
      runat="server"> 
     </asp:Label>   
      </td> 
      <td> 
       &nbsp;</td> 
     </tr> 
    </table> 
    </form> 
    <p> 
     &nbsp;</p> 
</body> 
</html> 

錯誤消息:

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

You can only have one <head runat="server"> control on a page. 
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: You can only have one <head runat="server"> control on a page. 

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace: 


[HttpException (0x80004005): You can only have one <head runat="server"> control on a page.] 
    System.Web.UI.HtmlControls.HtmlHead.OnInit(EventArgs e) +1762795 
    System.Web.UI.Control.InitRecursive(Control namingContainer) +333 
    System.Web.UI.Control.InitRecursive(Control namingContainer) +210 
    System.Web.UI.Control.InitRecursive(Control namingContainer) +210 
    System.Web.UI.Control.AddedControl(Control control, Int32 index) +198 
    System.Web.UI.ControlCollection.Add(Control child) +80 
    System.Web.UI.WebControls.WebParts.WebPartManagerControlCollection.AddWebPartHelper(WebPart webPart) +220 
    System.Web.UI.WebControls.WebParts.WebPartManagerControlCollection.AddWebPartsFromZone(WebPartZoneBase zone, WebPartCollection webParts) +607 
    System.Web.UI.WebControls.WebParts.WebPartManager.RegisterZone(WebZone zone) +240 
    System.Web.UI.WebControls.WebParts.WebZone.OnInit(EventArgs e) +95 
    System.Web.UI.WebControls.WebParts.WebPartZone.OnInit(EventArgs e) +9 
    System.Web.UI.Control.InitRecursive(Control namingContainer) +333 
    System.Web.UI.Control.InitRecursive(Control namingContainer) +210 
    System.Web.UI.Control.InitRecursive(Control namingContainer) +210 
    System.Web.UI.Control.InitRecursive(Control namingContainer) +210 
    System.Web.UI.Control.InitRecursive(Control namingContainer) +210 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +378 

回答

2

一個用戶控件嵌入在網頁中的內容,因此它不應該有一個頭部。只有你的實際頁面應該有一個標題。

它也不應該有任何其他文件部分,包括HTML,身體等。

0

你應該更換你的聲明:

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

或者你確實指它是你應該擺脫的自定義控件html和head標籤。

2

爲什麼你有一個完整的HTML結構 UserControl?無論何時在另一個頁面中使用此控件,您都將在另一個插入HTML文檔。只有包括需要使你的控件的HTML ...不是一整頁

編輯 ..想我措辭會更有意義的評論,而不是答案的方式。所以...到回答這個問題....你的控件有一個完整的html結構,所以當它在Webforms頁面中使用時,你有框架試圖在整個html結構中插入另一個。

只因爲你需要使你的.ascx控制把儘可能多的HTML ....不是一個完整的HTML結構

+0

感謝一堆傢伙 – somethingSomething

相關問題