How can I code VS.NET to format textboxes in a specific way?

I am creating an application in Visual Studio .NET that is connected to an Access database. I would like the textboxes in the application to have this format: $5,000.00. Where and how can I code it, so that when you run the application and the dataset fills it with the information from the database, it appears formatted as stated above?
If the textboxes reside in a DataGrid, DataList or Repeater, then the best way to do this is to use Web Server Control Templates (see ms-help://MS.VSCC.2003/MS.MSDNQTR.2003OCT.1033/vbcon/html/vbconwebformscontrolstemplates.htm in VS.NET's help system). By creating a template, you can specify how each item is to be displayed. For the particular grid items, you can specify an item template that looks something like this:
<asp:Label id=Label1 runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Field", "{0:C}")%>'> </asp:Label> </ItemTemplate>
Of course, if you have just a textbox you want to fill with a data-bound expression, you can simply do the following:
<asp:TextBox id=TextBox1 runat="server" 
   Text='<%# DataBinder.Eval(DataSet1, "Field", "{0:C}") %>'>
In both types of data binding, the third parameter to DataBinder.Eval is the format specification, in this case, using {0:C} means we want the value held in the DataSet's "Field" column to be displayed as currency.
This was first published in May 2004

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.



Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: