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


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>

Leave a comment

Please note that we won't show your email to others, or use it for sending unwanted emails. We will only use it to render your Gravatar image and to validate you as a real person.