Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two way databinding in ASP.NET

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.

like image 327
Alexander Avatar asked Sep 15 '11 09:09

Alexander


People also ask

What is two-way data binding example?

Two-way data binding refers to sharing data between a component class and its template. If you change data in one place, it will automatically reflate at the other end. For example, if you change the value of the input box, then it will also update the value of the attached property in a component class.

What is two-way binding MVC?

In two-way data binding, any changes in the Model gets reflected in the View and similarly any changes in the View gets reflected in the View automatically. This is done using attribute bind.

What is the difference between one-way and two-way data binding?

In one-way binding, the flow is one-directional. In a two-way binding, the flow is two-directional. This means that the flow of code is from ts file to Html file. This means that the flow of code is from ts file to Html file as well as from Html file to ts file.

What is databinding in oops?

Data binding is the process that establishes a connection between the app UI and the data it displays. If the binding has the correct settings and the data provides the proper notifications, when the data changes its value, the elements that are bound to the data reflect changes automatically.


1 Answers

Sadly to say that, but ASP.NET does not support two-way binding to .net objects... instead you can use something like "manual binding" on every post back (here AddIncomeSources is RepeaterControl)

public List<Income> AdditionalIncomeList 
{
    get { return ViewState["AdditionalIncome"] as List<Income>; }
    set { ViewState["AdditionalIncome"] = value; }
} 

foreach (RepeaterItem item in AddIncomeSources.Items)
{
    var amount = (TextBox)item.Controls.Cast<Control>().First(c => c.ID == "Amount");
    var document = (DropDownList)item.Controls.Cast<Control>().First(c => c.ID == "Document");
    AdditionalIncomeList[item.ItemIndex].Amount = amount.Text.ToDouble();
    AdditionalIncomeList[item.ItemIndex].IncomeDocument = document.SelectedValue;
}

AddIncomeSources.DataSource = AdditionalIncomeList;
AddIncomeSources.DataBind();
like image 144
Alexander Avatar answered Sep 21 '22 01:09

Alexander