James P. Wright
James P. Wright

Reputation: 9131

Month name from Int in Databound control in ASP.Net

First, I have some criteria I MUST meet. The question is whether this is possible and if so, how?

I have a databound control. My database has "Month" stored as an int. Is it possible for me to display the Month name (i.e. "January", "February", etc.) from the int WITHOUT using the code-behind?

Upvotes: 1

Views: 1486

Answers (4)

Wade K
Wade K

Reputation: 51

I recently needed to do the same thing, and my reply is for anyone for stumbles upon this article again.

Try using a Converter to convert the Month Integer into the correspo

{Binding StartDate.Month, Converter={StaticResource GetMonthName}}

The "GetMonthName" is a converter class that I created that returns the 3 digit abbreviation of the month using the integer value.

Upvotes: 0

Akram Shahda
Akram Shahda

Reputation: 14781

You can use DateName function to retrive the month name from the database:

DateName( month , DateAdd( month , MonthColumnName , 0 ) - 1 )

Upvotes: 0

Hanan
Hanan

Reputation: 1445

something like this .. just in case the other suggested methods won't do for you

do the databinding to some invisible control (maybe you can databind to a hidden-field)?

Now in effect databinding causes the page to reload in some way so you can activate some JavaScript code when that happens (i.e in OnLoad() event) and put the month name according to the Int where the conversion is done in a simple function you will make in JavaScript.

Upvotes: 0

Govind Malviya
Govind Malviya

Reputation: 13743

use this

<%# CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(Convert.ToInt32(Eval("ColumnName"))) %>

Upvotes: 1

Related Questions