Asp.net Calendar How to
I have a question about a project where I am working on it is a calendar where you can put a comment to a special day / year
I'm using atm the asp.net calendar control.
Here is the image of this idea:
Here is the Markup code:
<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Calender.master" CodeFile="Calender.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<form id="Form1" runat="server">
<asp:ScriptManager ID="SM1" runat="server" />
<asp:UpdatePanel ID="UP1" runat="server">
<ContentTemplate>
<asp:Calendar ID="Calendar1" ShowGridLines="true" CellPadding="15" runat="server"
OnDayRender="Calendar1_DayRender" OnSelectionChanged="Calendar1_SelectionChanged ">
</asp:Calendar>
<br />
<asp:FormView ID="FormView1" runat="server" AllowPaging="true"
DataKeyNames="date" DataSourceID="todoSrc" DefaultMode="Edit">
<EditItemTemplate>
<asp:Label ID="lblTodo" runat="server" AssociatedControlID="txtTodo" Text="Afspraak:" />
<br />
<asp:TextBox ID="txtTodo" runat="server" TextMode="MultiLine" Columns="30" Rows="5"
Text="<%# Bind('todo') %>"></asp:TextBox>
<br /><br />
<asp:LinkButton ID="butUpdate" runat="server" Text="Update" CommandName="Update" />
</EditItemTemplate>
<InsertItemTemplate>
<asp:Label ID="lblTodo" runat="server" Text="Toevoegen:" AssociatedControlID="txtTodo" />
<br />
<asp:TextBox ID="txtTodo" runat="server" Text="<%# Bind('todo') %>" TextMode="MultiLine"
Columns="30" Rows="5" />
<br />
<asp:Button ID="butInsert" runat="server" Text="Add" CommandName="Insert" />
</InsertItemTemplate>
</asp:FormView>
<br />
<asp:Button ID="butAddNew" runat="server" Text="Toevoegen" OnClick="butAddNew_Clic开发者_StackOverflowk" />
</ContentTemplate>
</asp:UpdatePanel>
<SelectParameters>
<asp:SqlDataSource ID="todoSrc" runat="server" ConnectionString="<%$ ConnectionStrings:Stefan %>"
DeleteCommand="DELETE Calender WHERE date=@date" InsertCommand="INSERT Calender (date,todo) VALUES (@date,@todo)"
SelectCommand="SELECT * FROM Calender WHERE date=@date" UpdateCommand="UPDATE Calender SET todo=@todo WHERE date=@date">
<SelectParameters>
<asp:ControlParameter Name="date" ControlID="Calendar1" PropertyName="SelectedDate" />
</SelectParameters>
<InsertParameters>
<asp:ControlParameter Name="date" ControlID="Calendar1" PropertyName="SelectedDate" />
</InsertParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="calendarSrc" runat="server" ConnectionString="<%$ ConnectionStrings:Stefan %>"
SelectCommand="SELECT date FROM Calender"></asp:SqlDataSource>
</form>
</asp:Content>
And here is the Codebehind cs file
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
DataView todo = new DataView();
protected void Page_Load(object sender, EventArgs e)
{
if (Calendar1.SelectedDate == DateTime.MinValue)
Calendar1.SelectedDate = Calendar1.TodaysDate;
}
void Page_PreRender()
{
todo = (DataView)calendarSrc.Select(DataSourceSelectArguments.Empty);
todo.Sort = "date";
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (todo.FindRows(e.Day.Date).Length > 0)
e.Cell.BackColor = System.Drawing.Color.Aqua;
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
FormView1.ChangeMode(FormViewMode.Edit);
}
protected void butAddNew_Click(object sender, EventArgs e)
{
FormView1.ChangeMode(FormViewMode.Insert);
}
}
Do you want the text directly in the cell of the target day?
Use the OnDayRender method that you already have to add a literal control:
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (todo.FindRows(e.Day.Date).Length > 0)
e.Cell.BackColor = System.Drawing.Color.Aqua;
if (e.Day.Date.Day == 18)
e.Cell.Controls.Add(new LiteralControl("<br />Test to show on the calendar."));
}
Good luck,
Patrick
精彩评论