I have a UserControl that takes a class object as property “DataSource.” On DataBind, I apply properties of that object to controls — TextBox, RadioButton, etc — inside the UserControl.
This works great when I just set the UC on a page, bind, and go.
Now, I’m attempting to use this control in the ItemTemplate of a ListView like so;
<ItemTemplate><uc1:MyItem ID="MyItem1" runat="server" DataSource='<%# Container.DataItem %>' /></ItemTemplate>
and bind to an array of those objects. The array is populated but I’m reaching my DataBind method of the UC with DataSource = null. Am I missing something?
EDIT: Holidays kept me away from this.
So, apparently I was calling base.DataBind() too late. My LoadForm(DataSource) method to load the object into the form fields preceded base.DataBind(). Swapping them fixed allowed me access to DataSource with no problem.
public override void DataBind()
{
base.DataBind();
LoadForm(DataSource);
}
Am I understanding my problem right? Was I just doing things out of order?
The “
<%# %>” syntax tells the compiler to run the associated code during the control’sDataBindingevent — butDataSourceneeds to be set before theDataBindingevent to be effective.One way to work around the issue might be to do your binding-related processing after the
DataBindevent runs, such as inPreRender.