Reputation: 226
I am going through a great book learning C# 5.0 and ASP.Net and came across data binding with controls. I have gone through the necessary steps to bind the data but I am receiving a confusing error message as the code below states.
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>
This is where the problem occurs, intellisense is telling me that show.ShowName is not in the current context, I don't know why as first it is exactly what the book presented and also why it would not be since Show.cs class is in the same folder? Any help solving this will be really appreciated. Thanks
Upvotes: 0
Views: 1252
Reputation: 648
Try making object of Show
as public
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();
}
}
}
Upvotes: 1
Reputation: 10460
Well, as Intellisense indicates, you can't access show
. You need to have a field defined as a part of the LabelText
class.
(BTW, something is wrong with your class name - is it LabelText
or Label
? I'll go with LabelText
since that's the name you have on the Inherits
attribute).
So this is what you need:
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();
}
}
}
Also, read this answer.
Upvotes: 1