Right align values in an ASP.NET DataList (WebForms)

Posted by Blake on 3/18/2013
)

I just wanted to share a simple example of how I was able to right align a set of numeric values in a DataList (that was nested in a repeater). When you’re allowing the framework to generate markup for you sometimes it becomes difficult to get the controls to render just as you want (if you were writing the markup yourself).

In this example I had to put a div inside of the ItemTemplate that had a CSS style set to “text-align: right;”.

<asp:DataList ID="dlAmount" runat="server" RepeatDirection="Vertical" HorizontalAlign="Right" Style="margin-left: -5px;">
    <ItemStyle VerticalAlign="Top" BorderStyle="None" HorizontalAlign="Right" />
    <ItemTemplate>
        <div style="text-align: right;">
            <asp:Literal runat="server" ID="litAmount" Text='<%# Databinder.Eval(Container.DataItem, "Amount")%>'></asp:Literal>
        </div>
    </ItemTemplate>                                                
</asp:DataList>