C# code for selecting only from todays date from a calendar - asp.net

Hi iam using a calendar control in my application,i need to select only the date from todays date and future date. am using this calendar control in the content place holder,can any one help me out with this problem.
Thanks in advance

Try this
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date < DateTime.Now.Date)
{
e.Day.IsSelectable = false;
}
}

Related

Disable Future Dates on asp:calendar control

How do I disable future dates on an asp:calendar control?
For example, today is October 22 2013, I want to disable dates from October 23 onward,
and re-enable them when it is the current date.
You will need to use the DayRender method.
C# Code Behind
public void Calendar1_DayRender(object o, DayRenderEventArgs e)
{
e.Cell.BackColor = System.Drawing.Color.Empty;
e.Cell.ForeColor = System.Drawing.Color.DarkRed;
//if the days are not part of the current month
if (e.Day.IsOtherMonth)
{
e.Cell.Text = "";
}
}
Then in the calendar, you need to place
ASP
<asp:Calendar ID="Calendar1" runat="server" Height="145px" Width="77px" OnDayRender="Calendar1_DayRender"></asp:Calendar>
You add the OnDayRender="Calendar1_DayRender" to the calendar control.
Hope this helps!
EDIT: I misread that. But if you want to just to "enable" the current day and no other days, then this will work..
Code Behind
public void Calendar1_DayRender(object o, DayRenderEventArgs e)
{
e.Cell.BackColor = System.Drawing.Color.Empty;
e.Cell.ForeColor = System.Drawing.Color.DarkRed;
if (e.Day.IsToday)
{
e.Cell.BackColor = System.Drawing.Color.Blue;
}
else
{
e.Cell.Text = "";
}
}
You can use this trick to disable future dates in Calendar Control.
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date > DateTime.Today)
{
e.Day.IsSelectable = false;
}
}

How to disable Current Date's Week in Calendar Control

I am trying to disable Current date's week in the calendar control. Can anyone tell me how to do this. For example... Today is August 21, 2012. I want that when calendar is loaded is should display the whole week from August 20 to August 25 as Disabled.
Hello you can use with ondayrender
<asp:Calendar ID="id" runat="server" ondayrender="Disabled_DayRender"></asp:Calendar>
protected void Disabled_DayRender(object sender, DayRenderEventArgs e)
{
if (condition) //specify your condition on yours days
{
e.Day.IsSelectable = false;
e.Cell.ForeColor = System.Drawing.Color.Gray;
}
}
You can calculate week number of year and then compare it with current date's week number.
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (GetNumber(e.Day.Date) == GetNumber(DateTime.Now.Date))
{
e.Day.IsSelectable = false;
e.Cell.ForeColor = System.Drawing.Color.Gray;
}
}
public static int GetNumber(DateTime dtDate)
{
System.Globalization.CultureInfo ciGetNumber = System.Globalization.CultureInfo.CurrentCulture;
int returnNumber = ciGetNumber.Calendar.GetWeekOfYear(dtDate, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Sunday);
return returnNumber;
}
This would do the trick. Thanks to Princy.

Customize Calendar control

I want to customize the calendar control in such a way that calendar starts from current date.
E.g. Today is 29th February then it should show the first date 9th February in the calendar.
Is it possible to do with asp.net calendar control?
By default the <asp:Calendar /> control displays the current month.
This can be overriden using the VisibleDate property if you like.
Could you please clarify what you mean that it should start from todays date? What should happen to the rest of the days of the month?
If you want to grey out or disable the other days in the month, hook into the OnDayRender event which will run when each day is rendered and it gives you access to the date and the cell so you can do something like.
protected void Calendar1_OnDayRender(object sender, DayRenderEventArgs e) {
if(e.Day.Date < DateTime.Now) {
e.Cell.CssClass="disabled";
}
}
The other property you need to know of is the SelectedDate property. Setting this is the equivalent of clicking on that particular date.
You ca set the SelectedDate propery. Assuming your calendar name is MyCal, you can set the first month date in this way:
myCal.SelectedDate = New Date(DateTime.Now.Year, DateTime.Now.Month, 1)
ADDENDUM
Try with this code:
protected void Page_Load(object sender, EventArgs e)
{
MyCal.SelectedDate = DateTime.Now.Date;
}
protected void MyCal_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date < DateTime.Now.Date)
{
e.Cell.Visible = false;
}
}
protected void MyCal_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
{
if (e.NewDate.Month == DateTime.Now.Date.Month - 1)
{
MyCal.SelectedDate = e.PreviousDate;
MyCal.VisibleDate = e.PreviousDate;
}
}
In this way you:
Set the current date
Hide the previous day
Disable previous month browsing

disable sunday in asp.net calender

How to disable Sunday in asp.net calendar control? I am using C#.
You can create a method for the dayrender event in the control. Something like this:
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date.DayOfWeek != DayOfWeek.Sunday) return;
e.Cell.ApplyStyle(new Style { BackColor = System.Drawing.Color.Gray });
e.Day.IsSelectable = false;
}

dropdown controls in asp.net

In my webpage I have two dropdown controls for namely ddlmonth and ddldays ( for month & days)
I write a method for current month in ddlmonth and current day in ddldays and i call that method in page load event it working fien but when i select (in ddlmonth ) different month and different day and insert into database the values inserted in database is current month and current day, actually i select different month and day
This is my code
This is the method for current date display in ddlmonth dropdwon control and ddlday
public void getMonth()
{
ddlmonth.SelectedIndex = DateTime.Now.Month -1;
ddldate.SelectedIndex = DateTime.Now.Day - 1;
}
I call this in page load
protected void Page_Load(object sender, EventArgs e)
{
getMonth();
}
If I call the getMonth method in postback
protected void Page_Load(object sender, EventArgs e)
{
if(!ispostback)
{
getMonth();
}
}
it not displaying the current month and date
without postback it is working
When I select different month and day and insert into database it is taking current month and day
Please help me
You can try to use Request.Form["ddlmonth"] and Request.Form["ddlday"] to retrieve the values.
Sorry if I misunderstood, but to "call the getMonth method in postback", you need check for ispostback = true, not false.
protected void Page_Load(object sender, EventArgs e)
{
if(isPostback)
{
getMonth();
}
}
I think u should use
protected void Page_Load(object sender, EventArgs e)
{
if(!ispostback)
{
getMonth();
}
}
The page is loaded and dropdowns showing Current date and month.
After that you choose different date and month and click some button to save that in database.
In the button event you write the insertion code and if required then there again call getMonth() method to load again the current date and month in the dropdowns. The date u chose in the dropdowns will be saved in DB. If required you call getMonth() method to load current date again as below.
protected void btnSave_Click(object sender, EventArgs e)
{
//-------- insertion code ----------------//
getMonth();
}

Resources