Thursday, November 27, 2008

Data Binding Expression


One can use objects as datasouce and bind to it by using GridView control.

<asp:Label ID="lbNumber" runat="server" Text='<%#Eval("Number") %>'></asp:Label>


Here "Number" is a property of the datasouce object.



If the field of datasouce is a strong typed object itself, we can also use like this:

<asp:Label ID="lbProjectType" runat="server" Text='<%#Eval("ProjectType.Type") %>'></asp:Label>


"ProjectType" is a property of datasouce object and is an object which has a member called "Type".



If we want to do some complex work during binding, we can also do it at code behind. One approach is writing code in RowDataBinding or ItemDataBinding event handler, The other way is define a method and call it declaratively. Below is an example for the later:


<asp:Label ID="lbScheduledDate" runat="server" Text='<%# DisplayDate(Eval("ScheduledFrom"), Eval("ScheduledTo")) %>'></asp:Label>

No comments: