Bind tag vs codebehind set, best practices

I would like to know what the best practice is to use the bind tag and set the direct property for the control in asp.net.


aspx.cs

public string Description {get; set;}

Aspx

<asp:Literal ID="txtDescription" runat="server" Text='<%# Description %>' />

aspx.cs

public string Description
{
    get { return txtDescription.Text ; }
    set { txtDescription.Text = value; }
}

Aspx

<asp:Literal ID="txtDescription" runat="server" />

The first of them is best to separate the design from the code, which gives the freedom to change an even identifier without breaking the code. But it looks like we can get a very long binding tag, for example, this short example:

Text='<%# ((fn_SearchReminders)Container.DataItem).dtDateActivation.Value.ToString("yyyy-MM-dd - hh:mm") %>'
+3
source share
2 answers

The only time it is worth using binding expressions is when ... databinding. For working with static controls, such as a text field, the best way to access it is how you did it in the second case.

Model View Presenter, aspx iSomeView, ,

string iSomeView.Description
{
    get { return txtDescription.Text ; }
    set { txtDescription.Text = value; }
}

, , :

Person iSomeView.Person
{
    get { return new Person { Name = txtName.Text, Phone = txtPhone.Text }; }
    set { txtName = value.Name; txtPhone.Text = value.Phone; }
}
+3

, GridView Repeater, , '/" > , '/" > "d" .

, , , , .

private void SetFormFields(Employee emp){
     lblName.Text = emp.Name;
     txtDateOfBirth.Text = emp.BirthDate.ToShortDateString();
}

.

0

All Articles