iT邦幫忙

0

使用aspx連結ascx編譯錯誤

我想要使用aspx連結ascx,參考了網路上面的一些文章,
但是在執行的時候還是會出現錯誤,
描述: 資源編譯無法完成 (錯誤發生於服務要求)。請檢閱下列的特定錯誤詳細資料,並視情況修改您的原始程式碼。
編譯器錯誤訊息: ASPNET: 請確定此程式碼檔中定義的類別符合 'inherits' 屬性,且其延伸正確的基底類別 (例如 Page 或 UserControl)。

請大大們幫我看看是哪邊出了問題,如有其他需要提供的,請再跟我說一聲,謝謝~

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" 
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="./WebUserControl.ascx"
TagPrefix="wuc" TagName="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication_WEBTEST
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }
}

WebUserControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" 
CodeFile="WebUserControl.ascx.cs" Inherits="Default" %>
<asp:Label ID="Label1" runat="server" Text="我是使用者自訂控制項">
</asp:Label>

WebUserControl.ascx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication_WEBTEST
{
    public partial class WebUserControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

執行後出現的錯誤訊息
https://ithelp.ithome.com.tw/upload/images/20191001/20107022Li9HdEfSKz.png

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
YoChen
iT邦研究生 1 級 ‧ 2019-10-01 11:55:14
最佳解答

您的ascx頁面的Inherits屬性,
請將值修改為WebApplication_WEBTEST.WebUserControl

看更多先前的回應...收起先前的回應...
YoChen iT邦研究生 1 級 ‧ 2019-10-01 11:56:38 檢舉

對了,記得要貼程式碼,不然很難抄~XDDD

Winnie iT邦新手 5 級 ‧ 2019-10-02 15:31:13 檢舉

我把ascx頁面的Inherits屬性已經改了,但還是出現一樣的錯誤....

ascx程式碼

<%@ Control Language="C#" AutoEventWireup="true" 
CodeFile="WebUserControl.ascx.cs" Inherits="WebApplication_WEBTEST.WebUserControl" %>
<asp:Label ID="Label1" runat="server" Text="我是使用者自訂控制項">
</asp:Label>
YoChen iT邦研究生 1 級 ‧ 2019-10-02 17:23:25 檢舉

請修改您aspx及ascx的Inherits屬性

aspx

Inherits="WebApplication_WEBTEST.Default"

ascx

Inherits="WebApplication_WEBTEST.WebUserControl"
Winnie iT邦新手 5 級 ‧ 2019-10-03 11:47:29 檢舉

可以了喔~謝謝~

我要發表回答

立即登入回答