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?
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:
or if you’re binding to a DataTable, something like:
then you can have a consistenly formatted order id anywhere in your markup: