2011-11-19 147 views
0
Compiler Error Message: CS0122: 'Controls_Arcade_StarDisplay.Stars' is inaccessible due to its protection level 

我檢查了其他SO線程,但無法對它們有很大的意義。此控件正在另一個webusercontrol中使用。每當我嘗試用它做任何事情時,都會拋出這個錯誤。有問題的階級是這樣的:由於其保護級別,Webusercontrol無法訪問

<pages validateRequest="false" smartNavigation="false" clientIDMode="Static"> 
     <controls> 
      <add tagPrefix="Scirra" src="~/Controls/Arcade/GameOverview.ascx" tagName="GameOverview"/> 
      <add tagPrefix="Scirra" src="~/Controls/Arcade/Stars/StarDisplay.ascx" tagName="StarDisplay"/> 
     </controls> 
    </pages> 

開始顯示控制遊戲概述內出現:

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

public partial class Controls_Arcade_StarDisplay : System.Web.UI.UserControl 
{ 
    public double Rating { get; set; } 
    public string Tooltip { get; set; } 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     string CSSClass = "star-rating s-"; 
     if (Rating < 0.25) 
      CSSClass += "0"; 
     else if (Rating < 0.75) 
      CSSClass += "05"; 
     else if (Rating < 1.25) 
      CSSClass += "1"; 
     else if (Rating < 1.75) 
      CSSClass += "15"; 
     else if (Rating < 2.25) 
      CSSClass += "2"; 
     else if (Rating < 2.75) 
      CSSClass += "25"; 
     else if (Rating < 3.25) 
      CSSClass += "3"; 
     else if (Rating < 3.75) 
      CSSClass += "35"; 
     else if (Rating < 4.25) 
      CSSClass += "4"; 
     else if (Rating < 4.75) 
      CSSClass += "4"; 
     else if (Rating < 1.25) 
      CSSClass += "1"; 
     else if (Rating < 4.75) 
      CSSClass += "45"; 
     else 
      CSSClass += "5"; 
     Stars.CssClass = CSSClass; 

     if (Tooltip != null) 
      Stars.ToolTip = Tooltip; 
    } 
} 

控制在web.config中註冊:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="StarDisplay.ascx.cs" Inherits="Controls_Arcade_StarDisplay" %> 
<asp:Panel runat="server" ID="Stars" /> 

代碼背後控制。

感謝您的幫助,完全卡住!其他問題指的是獲取我不理解的控件的屬性,或者改變設計器中我無法找到的東西。

回答

2

這意味着您的用戶控件中有一些名爲Stars的文件,它可能定義爲privateprotected

的問題是,這是一個網站,而不是一個Web應用程序和Web站點,.NET會自動創建runat="server"控件protected屬性,因此它們不能在用戶控件或它的直接繼承人的外部暴露出。

因此有兩個可能的選擇:

  1. 轉換網站的Web應用程序。這將導致創建包含控件定義的設計器文件。然後,您可以將控件從設計器文件中取出,放入代碼隱藏文件中,並將訪問級別更改爲公開。

  2. 由於這是一個可以從其他控件訪問的控件,比直接公開控件更好的設計是提供交互方法,其他控件可以使用這些方法來封裝此控件中的行爲。例如,公開接受評級的SetRating方法並在內部更新Stars面板。

從設計的觀點來看,選項#2絕對是更好的選擇。

+0

我發佈了整個班級,將'protected void Page_Load'更改爲'public void Page_Load'會引發同樣的錯誤 –

+0

Stars的聲明在設計器文件中是什麼樣的? –

+0

對不起,我以前從未真正使用過設計器,設計器文件位於何處? –

0

我有我的控制相同的錯誤消息。我檢查了我的代碼文件,答案變得明顯。該程序被定義爲void Register_Submit()。我已將public添加到其定義中,因此它變成了Public void Register_Submit(),錯誤消失了。

相關問題