Formatting databound asp.net dropdown
I have an asp.net webpage with a drop down menu that i开发者_Python百科s populated by a databound list that pulls it's entries (dates) from a database.
I have this part working and displaying the correct information, but the thing is that it displays the dates as mm/dd/yyyy hh:mm:ss AM/PM. Ideally, I would like it to display as mm/yyyy (even just mm/dd/yyyy would be ok).
Does anyone know how to do this?
Thanks.
The simplest approach is to do it in the Select statement - use the Convert() function (assuming the DB is SQL Server)
Select Convert(varchar, MyDateField, 101) AD MyDateField
Usage and different formats (alternatives to "101" in the example above) can be found here: http://msdn.microsoft.com/en-us/library/ms187928.aspx
Alternately, you can use the DataTextFormatString property: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.datatextformatstring.aspx
and set it to "{0:dd/MM/yyyy}"
You can try this in your code behind:
dropdownlistname.DataTextFormatString ="{0:MM/yyyy}"
or in the aspx form:
<asp:DropDownList ID="dropdownlistname"
runat="server"
DataTextFormatString='{0:MM/yyy}'......>
精彩评论