Search code examples
c#asp.netdata-bindingwebformsdatabinder

Bind label to DataSource.TotalRowCount


I have a custom DataSourceControl class that I'm using somewhat like a view model. I'm coming from WPF databinding land and I would like to be able to bind the text of a label to the TotalRowCount using a databinding expression. I can update the label from the code-behind, but that's not very MVVM. This sample generates a label with blank text:

<cc:EquipmentDataSource ID="edsEquipment" runat="server"></cc:EquipmentDataSource>
<asp:Label ID="Label1" runat="server" text='<%# Eval("edsEquipment.TotalRowCount") %>'></asp:Label>

Am I misunderstanding something about how the databinding works? I was getting the same empty string when I used an ObjectDataSource as well.


Solution

  • Based on this answer, it would appear two things need to change:

    1. You don't need the Eval, the value of the Text attribute can be the property name on your data object.
    2. You need to call Page.DataBind() in your code behind.

    Hope that helps!