Two way databinding in ASP.NET
-
30-10-2019 - |
سؤال
Let say that we have an object
class Entity
{
public string ID {get; set;}
public string Name {get; set;}
}
I want to bind properties to two textboxes on a page something 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 behind
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 values in textboxes I expect EntityObject to have these values in properties when page reloads after PostBack, but properties are always null.
لا يوجد حل صحيح
لا تنتمي إلى StackOverflow