Reputation: 1790
I feel ashamed, im doing something wrong and i have no idea what it is. Somebody here to help? If so, thanks in advance!
I am trying to read a property from the Inherited Code in the GAC from an ASP Usercontrol. The CodeBehind in the UserControl is:
namespace My.Name.Space
{
public class MyControl : UserControl
{
public String SomeString { get; set; }
public Boolean MyProperty
{
get
{
if (String.Equals(SomeString,"SomeValue"))
return true;
return false;
}
}
// other code
}
}
The ascx looks like this:
<%@ Control ClassName="MyControl" Language="C#" Inherits="My.Name.Space.MyControl, MyDLL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxx" %>
<asp:DropDownList runat="server" id="myId" Visible='<%# Eval("MyProperty") %>' />
The Control is hard wired into an Aspx Page but the SomeString Property aint set in the aspx! the SomeString Property is set in the OnLoad of the Aspx Page.
Any Idea whats wrong with my Code? Thank again!
edit: i also tried <asp:Label Text='<% Eval("MyProperty") %>' />
, same Error (An unexpected error has occurred. )
Upvotes: 2
Views: 1682
Reputation: 15663
The Eval code block is used with databinding controls, but is also evaluated when this.DataBind() is called.
However you don't need to use Eval (actually you can't with an user control) since this construct is translated by ASP .Net to something like DataBinder.Eval(someObject, "SomePropertyOfAnObject")
which takes the someObject and via reflection gets the value of SomePropertyOfAnObject property. DataBinding controls have a DataSource property which is a collection of objects that are going to be evaluated. With user controls is not the case.
You can still use the <%# %>
, but in the following way:
<asp:DropDownList runat="server" id="myId" Visible='<%# MyProperty %>' />
ASP .Net will generate something like:
dataBindingExpressionBuilderTarget.Visible = ((bool)( MyProperty ));
Which means the MyProperty should be at least protected, if you change your mind in future.
Upvotes: 1
Reputation: 619
To use the <%# %> syntax in the standard markup (outside binded control) - you must call DataBind() on the user control or page itself.
Upvotes: 1