Wednesday, 20 July 2011

Using the Calendar Control with ASP.NET 4.0 and C#


This tutorial will demonstrate how to use the calendar control with ASP.NET 4.0 and C#.

Adding the Default.aspx Page
To work with the calendar control we will be creating a simple web page that will contain a label and a calendar. At this point I have created a new ASP.NET Empty Web Site. Next, we need to:
  1. Right click the project in your solution explorer.
  2. Select add new item...
  3. Select a web form.
  4. Name it 'Default.aspx'.
  5. Click add.
  6. Open Default.aspx up to design mode.
  7. Drag and drop a label onto the web form.
  8. Add in a break line under the label.
  9. Drag and drop a calendar underneath the label.
We used over 10 web hosting companies before we found Server Intellect. Our new cloud server was set up in less than 24 hours. We were able to confirm our order over the phone. They responded to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

Working with the Calendar
Now that we have our simple form setup we can begin working with the calendar. The calendar allows you to select dates and even has some custom events to tell when the user is working with the calendar control. Here we will simply demonstrate how to determine which date has been selected by the user in the event that the selected date on the calendar has been changed. To begin, open up Default.aspx to design mode and:
  1. Double click the calendar to generate the Calendar1_SelectionChanged event method.
  2. Add the following code to the Calendar1_SelectionChanged event method:
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        //display the selected date of the calendar
        Label1.Text = Calendar1.SelectedDate.ToString();
    }
This code will simply take the SelectedDate property from our calendar and display it in our label.

Yes, it is possible to find a good web host. Sometimes it takes a while to find one you are comfortable with. After trying several, we went with Server Intellect and have been very happy thus far. They are by far the most professional, customer service friendly and technically knowledgeable host we've found so far.

Testing
To test this out and ensure that it is working properly, load up the web site and select a date in the calendar. The page should do a complete postback and load with the currently selected date in the label.

No comments:

Post a Comment