views:

40

answers:

1

When are declared values bound to properties of a user control in WebForms?

I have a user control which has a public property which is an enum type. In my aspx page I'm setting it's value declaratively. In the ascx I'm outputting the value to the page using <%= %> syntax. The value that is output by the echo syntax is always zero 0. The enum does nopt have a zero value.

My question therefore is, is echo <%= %> syntax evaluated before the declared value is bound to the property?

Example:

public enum Foo
{
    Bar = 1,
    Bahh = 2,
    BlackSheep = 3
}

// MyUserControl.cs
public class MyUserControl : UserControl
{
   public Foo Fizz { get; set; }
}

// MyUserControl.ascx
<a href="foo.aspx?foo=<%= this.Fizz %>">Foo</a>

// MyPage.aspx
<foo:MyUserControl runat="server" ID="foo:MyUserControl1" Fizz="Bar" />
<foo:MyUserControl runat="server" ID="foo:MyUserControl2" Fizz="Bahh" />
<foo:MyUserControl runat="server" ID="foo:MyUserControl3" Fizz="BlackSheep" />

The output is :

<a href="foo.aspx?foo=0">Foo</a>
<a href="foo.aspx?foo=0">Foo</a>
<a href="foo.aspx?foo=0">Foo</a>

I'm going to assume that zero is the "unset" value for any enum member and therefore assume that <%= is executed before the value is bound to the property.

+2  A: 

Zero is the default value and yes the markup on your usercontrol is evaluated before the declarative setting. You should set the value on the page_init event

Ricardo Rodrigues