Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.Net用户控件数据绑定错误-属性在当前上下文中不存在_C#_Asp.net - Fatal编程技术网

C# ASP.Net用户控件数据绑定错误-属性在当前上下文中不存在

C# ASP.Net用户控件数据绑定错误-属性在当前上下文中不存在,c#,asp.net,C#,Asp.net,我正在读一本学习C#5.0和ASP.Net的好书,遇到了带有控件的数据绑定。我已经完成了绑定数据的必要步骤,但是我收到了一条令人困惑的错误消息,如下代码所示 Show.cs //Class for the show properties namespace BindingExample { public class Show { public int ID {get; set;} public String ShowName {get; set;}

我正在读一本学习C#5.0和ASP.Net的好书,遇到了带有控件的数据绑定。我已经完成了绑定数据的必要步骤,但是我收到了一条令人困惑的错误消息,如下代码所示

Show.cs //Class for the show properties

namespace BindingExample
{
   public class Show 
   {
      public int ID {get; set;}
      public String ShowName {get; set;}
   }
}

LabelText.aspx.cs //user control code behind

namespace BindingExample
{
  public partial class Label LabelText: System.Web.UI.Page
  {
     protected void load_page(object sender, Event args)
     {
        Show show = new Show
        {
           ID = 1,
           ShowName = "C# is the Best"
        };

       //binding
       Page.Binding();
     }
   }

LabelText.aspx //user control markup where the problem occurs.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LabelText.aspx.cs" Inherits="BindingExample1.LabelText" %>

<!DOCTYPE html>

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

        <asp:Label ID="Label1" runat="server" Text="<%# show.ShowName%>"></asp:Label>

    </div>
    </form>
</body>
</html>
Show.cs//Show属性的类
名称空间绑定示例
{
公开课
{
公共int ID{get;set;}
公共字符串ShowName{get;set;}
}
}
LabelText.aspx.cs//用户控制代码隐藏
名称空间绑定示例
{
公共部分类标签LabelText:System.Web.UI.Page
{
受保护的无效加载页面(对象发送方、事件参数)
{
Show=新节目
{
ID=1,
ShowName=“C#是最好的”
};
//装订
Page.Binding();
}
}
LabelText.aspx//出现问题的用户控件标记。

这就是问题发生的地方,intellisense告诉我show.ShowName不在当前上下文中,我不知道为什么它首先是这本书介绍的内容,也不知道为什么它不是,因为show.cs类在同一个文件夹中?任何解决此问题的帮助都将不胜感激。谢谢您无法访问
show
。您需要将一个字段定义为
LabelText
类的一部分

(顺便说一句,您的类名有问题-是
LabelText
还是
Label
?我将使用
LabelText
,因为这是您在
继承属性上的名称)

这就是你需要的:

namespace BindingExample
{
    public partial class LabelText: System.Web.UI.Page
    {
        protected Show _show;
        protected void load_page(object sender, Event args)
        {
            _show = new Show
            {
                ID = 1,
                ShowName = "C# is the Best"
            };

            Page.DataBind();
        }
    }
}

另外,请阅读答案。

正如Intellisense所指出的,您不能访问
show
。您需要将一个字段定义为
LabelText
类的一部分

(顺便说一句,您的类名有问题-是
LabelText
还是
Label
?我将使用
LabelText
,因为这是您在
继承属性上的名称)

这就是你需要的:

namespace BindingExample
{
    public partial class LabelText: System.Web.UI.Page
    {
        protected Show _show;
        protected void load_page(object sender, Event args)
        {
            _show = new Show
            {
                ID = 1,
                ShowName = "C# is the Best"
            };

            Page.DataBind();
        }
    }
}

另外,阅读答案。

尝试将
的对象显示为公共对象

namespace BindingExample
{
    public partial class LabelText: System.Web.UI.Page
    {
        public Show _show; //this will allow this object to be used in aspx page
        protected void load_page(object sender, Event args)
        {
            _show = new Show
            {
                ID = 1,
                ShowName = "C# is the Best"
            };

            Page.DataBind();
        }
    }
}

尝试将
的对象显示为公共对象

namespace BindingExample
{
    public partial class LabelText: System.Web.UI.Page
    {
        public Show _show; //this will allow this object to be used in aspx page
        protected void load_page(object sender, Event args)
        {
            _show = new Show
            {
                ID = 1,
                ShowName = "C# is the Best"
            };

            Page.DataBind();
        }
    }
}

感谢@Pratik和HeyJude两个解决方案都非常有效!感谢@Pratik和HeyJude两个解决方案都非常有效!感谢@Heyjudethak you@HeyJude