asp.netdata-bindinginline-code

inline code on server control properties


I have a public POCO property (SiteDetail) on my page and I need to know what's the best approach when setting properties of server controls:

  1. Use inline code and Page.DataBind(); on load

    <asp:Label ID="lbName" runat="server" Text="<%# SiteDetail.Name %>"/>

  2. Do not use inline code and set control properties on page load

    lbName.Text = SiteDetail.Name;

Is it "dangerous" to use Page.DataBind() on load?


Solution

  • So...I did some more research and found Page.DataBind() is not a good thing, is better to call DabaBind on every single control you need, as @Muhammad Akhtar says, both ways renders the same so I prefer to use inline code because it seems clearer, now I have

    <asp:Label ID="lbName" runat="server" Text="<%# SiteDetail.Name %>"/>
    

    and code behind:

    if (!IsPostBack)
    {
            lbName.DataBind();
    }