views:

56

answers:

1

Say I have a label inside a gridview template column and I assign the text to the databound item, "OrderID". Is there any way I can call .ToString().PadLeft(7, '0') ? Here's the label as it stands:

<asp:Label ID="lblOrderID" runat="server" Text='<%# "WW" +  DataBinder.Eval(Container.DataItem, "OrderID") %>' />

Because PadLeft requires a char, I can't use my single quotes because I've already surrounded the value in single quotes and can't do double quotes because of the DataBinder.Eval expression. Is there another way I can get this to work on one line?

+1  A: 

You can do it inline (I'll leave it to someone else to give you the answer as I don't have VS open to test it).

But I would do it by adding a method in the codebehind to return a formatted order id. Or even better, put the format method in a static helper class so it's available to any page that needs a formatted order id.

E.g. if you're binding to a collection of Orders, something like:

public static string GetFormattedOrderId(object dataItem)
{
    Order order = (Order) dataItem;
    return String.Format("WW{0:N7}", order.OrderId);      \
    // or return order.OrderId.ToString().PadLeft... if you prefer
}

or if you're binding to a DataTable, something like:

public static string GetFormattedOrderId(object dataItem)
{
    DataRow row = (DataRow) dataItem;
    return String.Format("WW{0:N7}", row["OrderId"]);      
}

then you can have a consistenly formatted order id anywhere in your markup:

'<%# MyFormattingHelper.GetFormattedOrderId(Container.DataItem) %>'
Joe