I would like to know what is the best practice between using bind tag and setting direct property to a 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 being best to separate design from code giving the liberty of changing even ID without breaking code. But it seems we can get very long bind tag sometime like this really short example:
Text='<%# ((fn_SearchReminders)Container.DataItem).dtDateActivation.Value.ToString('yyyy-MM-dd - hh:mm') %>'
The only time it’s worth using the binding expressions is when… databinding. For dealing with static controls like your textbox the best way to access it is the way you did in the second case.
This is even the case for dealing with the Model View Presenter implementation where generally your aspx page will inherent from iSomeView and you will access properties similar to
Using a method similar to this also allows you to construct complex objects easily: