【问题标题】:Webusercontrol is inaccessible due to its protection levelWebusercontrol 由于其保护级别而无法访问
【发布时间】:2012-01-01 18:15:06
【问题描述】:
Compiler Error Message: CS0122: 'Controls_Arcade_StarDisplay.Stars' is inaccessible due to its protection level

我检查了其他 SO 线程,但无法理解它们。此控件正在另一个 webusercontrol 中使用。每当我尝试用它做任何事情时,它都会抛出这个错误。有问题的课程是这样的:

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

后面的代码:

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中注册:

    <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>

开始显示控件出现在游戏概览控件中。

感谢您的帮助,完全卡住了!其他问题涉及获取我不理解的控件的属性,或者更改我也找不到的设计器中的内容。

【问题讨论】:

    标签: c# asp.net webusercontrol


    【解决方案1】:

    这意味着您的用户控件中有一个名为Stars 的东西,它可能定义为privateprotected

    问题在于这是一个网站而不是 Web 应用程序,在网站中,.Net 会自动将 runat="server" 控件创建为 protected 属性,因此它们不能暴露在用户控件或其外部直接继承人。

    所以有两种可能的选择:

    1. 将网站转换为 Web 应用程序。这将导致创建包含控件定义的设计器文件。然后,您可以将控制权从设计器文件中取出,将其放在您的代码隐藏中,并将访问级别更改为公共。

    2. 由于这是一个可以从其他控件访问的控件,因此比直接公开控件更好的设计是提供其他控件可以使用的交互方法,以便将行为封装在该控件中。例如,公开一个接受评级并在内部更新Stars 面板的SetRating 方法。

    从设计的角度来看,选项 #2 绝对是更好的选择。

    【讨论】:

    • 我发布了整个班级,将protected void Page_Load 更改为public void Page_Load 会引发同样的错误
    • 设计师文件中Stars的声明是什么样子的?
    • 对不起,我以前从未真正使用过设计器,设计器文件在哪里?
    • 我认为它是默认生成的。为了避免这种情况,我通常会做的是在常规代码隐藏(而不是设计器)中公开公共属性或方法,并以这种方式共享控件。根据您的情况,这可能是也可能不是。
    • 两种方法:1)在页面上右击选择View Code Gen File; 2)显示项目中的所有文件,然后展开你的页面;你会看到它和你的代码隐藏。
    【解决方案2】:

    我的控件出现了相同的错误消息。我检查了我的代码文件,答案很明显。该过程被定义为void Register_Submit()。我在它的定义中添加了public,所以它变成了Public void Register_Submit(),并且错误消失了。

    【讨论】:

      猜你喜欢
      • 2015-09-26
      • 2011-09-01
      • 1970-01-01
      • 2011-04-02
      • 2011-02-07
      • 2012-01-25
      • 2022-01-18
      相关资源
      最近更新 更多