JohnDoe4136
JohnDoe4136

Reputation: 539

Show dates in SQL in asp:Calendar

I am wondering if it's possible to have a set of dates in a SQL db and display in in a <asp:Calendar ID="Calendar1" runat="server"> for a user to select the dates available in a detailsview. Or if there's a better idea of displaying the dates?

Thanks.

Upvotes: 1

Views: 549

Answers (1)

J0e3gan
J0e3gan

Reputation: 8938

To my knowledge, you cannot selectively remove dates from the ASP.NET Calendar control, but you can selectively disable dates by handling the DayRender event: just disable dates that are not in the set of dates returned from the database.

You might find a couple online examples helpful taking this approach - one in Bean Software's ASP.NET FAQ, another in DotNetSpider's forums.

Here is my stab at what you will need:

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
    // Disable the date being rendered if it is not in the DB-returned dates (as
    // List<DateTime> _databaseDates).
    if (!_databaseDates.Contains(e.Day.Date))
    {
        e.Day.IsSelectable = false;
        e.Cell.ForeColor = Color.Gray; // or e.Cell.Font.Strikeout = true;
    }
}

Upvotes: 1

Related Questions