asp.net用户控件绑定

时间:2010-11-30 00:50:13

标签: asp.net page-lifecycle

我在asp.net中创建了一个带有自定义属性的用户控件,如下所示。在使用此用户控件的aspx页面中,我有类似以下内容的内容。用户控件位于FormView的EditItemTemplate

<uc1:MyControl ID="c1" runat="server" CountryCode='<%# this.DropDownCountry.SelectedValue %>' Value1='<%# Bind("Value1Col") %>' />

我正在尝试在用户控件的CountryCode方法中使用Page_Load,但未填充该值。

我的问题是在控件的生命周期的哪个阶段,有界值会被填充?我尝试直接分配值,如下所示,它确实在Page_Load方法中得到它的值。

<uc1:MyControl ID="c1" runat="server" CountryCode="CA" Value1='<%# Bind("Value1Col") %>' />

感谢,

uc1:MyControl 具有以下属性:

[Browsable(true)]
[Bindable(true, BindingDirection.TwoWay)]
[DefaultValue(0)]
[PersistenceMode(PersistenceMode.Attribute)]
public string CountryCode
{
    get { return _countryCode; }
    set { _countryCode = value; }
}

1 个答案:

答案 0 :(得分:0)

由于您正在使用数据绑定语法,因此需要在控件上调用DataBind方法 - 您可以将其添加到aspx页面的Page_Load方法中,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    c1.DataBind();
}