Monday, June 22, 2009

TextBox control (ASP.NET) to accept only numeric values

Very often for data entry screens in a web-application, we have a requirement for having text-box control to accept only numeric characters.. there is no ready-made control available with available server controls.. but there are couple of simple ways in which we can achieve this kind of behavior..

1. simply use a textbox control, and put a regular expression validator on this box.. with regular expression as \d+.. and you are done..

2. use a simple javascript function to do the same, and grab the keypress event for the box..

function blockNumber(e)
{
var keychar;
var reg;
keychar = String.fromCharCode(e.keyCode);
reg = /\d/;
return reg.test(keychar);
}


this function will block user from entering any character other then numbers.. add this function call as attribute for onkeypress event...

txtMyBox.Attributes.Add("onkeypress", "return blockNumber(event);");


and you are done..

isn't this simple or what!!! :)

Monday, June 8, 2009

Managing Holiday List (Calendar Control ASP.NET)

typical requirement.. to manage custom holiday list to be used in some SLA calculations.. and this was suppose to be conventional web based..
Before this I never played that much with ASP.NET Calendar control.. so I decided to give it a shot..& see if requirement can be met.. and to my surprise.. it shaped-up real fast & good by the late afternoon.. in just couple of hours I had this screen up-n-running!!! :D

steps --- 1. created a table (HolidayList) in DB with single field as Holiday Date.
2. drag asp.net calendar control on the form (refer image above.. left middle side.. )
3. get the holiday list collection from HolidayList table (from DB)...
4. Few events which will make the thing going...
a. OnDayRender - this event is fired when a day is getting rendered on calendar.. & gives 'DayRenderEventArgs e', having the day/cell/url for each day which is getting rendered.. all you need is to check the day with the holiday list.. if matched.. change the look & feel of the cell (viz. Table Cell)..
e.Cell.BackColor = System.Drawing.Color.Silver;
e.Cell.ForeColor = System.Drawing.Color.Black;
e.Cell.Font.Bold = true;

b. OnVisibleMonthChanged - fired when month is changed by user.. all we need to do is to get data for this month.. & keep the collection for DayRendering..
c. OnSelectionChanged - fired when date selection is changed by user.. this event gives me the opportunity to capture the selected date and store it in DB (Holiday List table).. refresh the holiday list collection..
5. dragged a ListBox server control to help me with removal of dates from the list.. allowing multiple selection.. and remove button.. simply check for selected dates & remove them from data table (holiday list.. )
foreach (ListItem lstItem in lstHolidays.Items)
{
if (lstItem.Selected)
{
DateTime dt = Convert.ToDateTime(lstItem.Text);
hlfactory.Delete(dt);
}
}


few other supporting operations required (for data insertion/deletion etc.)... & you are done.. it is this simple!!!