I'm working with webforms and dynamic data and need to pass an object from the code behind my dynamic controls to my field template.
I have successfully tried this using a string property called Test. To demonstrate, this control is on my form...
<asp:DynamicControl Test="Hello field" id="MyID" runat="server" DataField="MyField" Mode="Edit" />
... where MyField
is a string property of the bound object, and my String_Edit.ascx
field template has the following property...
public string Test { get; set; }
... I can see that this property Test
holds the value I supplied i.e. "Hello field"
when I break at its Page_Load
method.
What I actually want though is to pass a more complex object which is referenced as a property in my code behind. But if I try this...
<asp:DynamicControl Test="<%# this.MyObject %>" id="MyID" runat="server" DataField="MyField" Mode="Edit" />
... where the code-behind has this property...
protected MyClass MyObject
{
get
{
if (this.o == null)
{
this.o = new MyClass();
}
return this.o;
}
}
... and my String_Edit.ascx
has its property changed to...
public MyClass Test { get; set; }
... then this property Test
is always null
.
I have a feeling that the problem is something to do with having to binding the control, but when I try this I get exceptions along the lines of "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control".
Update
I've since found that even the following code...
<asp:DynamicControl Test="<%# Eval("MyProperty") %>" id="MyID" runat="server" DataField="MyField" Mode="Edit" />
...where MyProperty
is a string property of the bound object, even that doesn't end up populating the Test property of String_Edit.ascx
. Even though when I debug I can see MyProperty
being accessed.
Any suggestions most welcome.
this is not possible you can only pass in "value types" via the markup. note this is not a limitation of Dynamic Data but Asp.Net WebForms sorry.