Reputation: 101
I have added a user control into a form view item template, but I have added a public property to the user control, and I can't seem to figure out how to set the property. I have tried the following:
<uc1:OfacControl id="OfacControl1" runat="server" AssetEvictionId='<%# Bind("AssetEvictionId") %>' />
But the value never gets set correctly.
I have also tried doing it in the codebehind of the form in the preRender method like so:
var assetEvictionIdHiddenField = (HiddenField)oFormView.FindControl("AssetEvictionIdHiddenField");
var OfacControl1 = (Ofac)oFormView.FindControl("OfacControl1");
if (OfacControl1 != null && assetEvictionIdHiddenField != null)
OfacControl1.AssetEvictionId = Convert.ToInt32(assetEvictionIdHiddenField.Value);
This doesn't work either.
It appears to be a timing issue. It looks like the control is being rendered at a different time than the value is being set. If the bind syntax doesn't work, and I am forced to use the code behind, which form view event should I be using to set the value. I have also tried the OnItemCreated event. This didn't work either.
Upvotes: 1
Views: 616
Reputation: 20415
It seems you are trying to set the control in the wrong place. You should be setting it in Init.
ASP.NET Page Life Cycle Overview states the following:
Raised after all controls have been initialized and any skin settings have been applied. The Init event of individual controls occurs before the Init event of the page. Use this event to read or initialize control properties.
Upvotes: 1