Say we have an object
class Entity
{
public string ID {get; set;}
public string Name {get; set;}
}
I want to bind properties to two text fields on a page like this:
<asp:FormView ID="FormView" runat="server">
<ItemTemplate>
<asp:textbox ID="TextId" Text='<%# Bind("ID") %>'/>
<asp:textbox ID="TextId" Text='<%# Bind("Name") %>'/>
</ItemTemplate>
</asp:FormView>
and then write this in code for
public EntityObject
{
get { return ViewState["Entity"] as Entity; }
set { ViewState["Entity"] = value; }
}
protected override void OnInit(EventArgs e)
{
if (EntityObject== null)
EntityObject= new EntityObject();
FormView.DataSource = new[] { EntityObject };
FormView.DataBind();
base.OnInit(e);
}
And when I enter the values in the text fields, I expect EntityObject to have these values in the properties when the page reloads after PostBack, but the properties are always zero. Please help, where am I mistaken?
source
share