1

在此处输入图像描述

Label lbl = new Label();
lbl.BorderColor = System.Drawing.Color.Black;
lbl.BorderStyle = BorderStyle.Double;
lbl.BackColor = System.Drawing.Color.BlanchedAlmond;
lbl.Text = eventname + "<br/>" + dt + "--" + dt1;
e.Cell.Controls.Add(lbl);

btn.Click += new System.EventHandler(this.btnCross_Click);
e.Cell.Controls.Add(btn);

public void btnCross_Click(object sender, EventArgs e)
{
    string id = ((Button)sender).ID;
}

我的数据库字段是 id, name , eventstartdate , eventenddate

我应该如何完成这个我的日历控件在 UpdatePanel 中,这就是为什么这个 btnCross_Click 没有触发

4

1 回答 1

1

下面代码的想法是在 Page_Load 方法中创建所有必需的控件,并将它们进一步移动到适当的容器中。

<asp:Calendar runat="server" ID="Calendar1" Width="800px" Height="700px" BackColor="White"
    BorderColor="Black" DayNameFormat="Shortest" Font-Names="Times New Roman" Font-Size="10pt"
    ForeColor="Black" NextPrevFormat="FullMonth" TitleFormat="Month" ShowGridLines="true" >
    <DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" ForeColor="#333333"
        Height="10pt" />
    <DayStyle Width="14%" />
    <NextPrevStyle Font-Size="8pt" ForeColor="White" />
    <OtherMonthDayStyle ForeColor="#999999" />
    <SelectedDayStyle BackColor="#CC3333" ForeColor="White" />
    <SelectorStyle BackColor="#CCCCCC" Font-Bold="True" Font-Names="Verdana" Font-Size="8pt"
        ForeColor="#333333" Width="1%" />
    <TitleStyle BackColor="Black" Font-Bold="True" Font-Size="13pt" ForeColor="White"
        Height="14pt" />
    <TodayDayStyle BackColor="#CCCC99" />
</asp:Calendar>
<asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>

代码隐藏:

private Dictionary<DateTime, List<Control>> DayControls = new Dictionary<DateTime, List<Control>>();

protected void Page_Init(object sender, EventArgs e)
{
    Calendar1.DayRender += new DayRenderEventHandler(Calendar1_DayRender);
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if(Calendar1.VisibleDate == DateTime.MinValue)
        {
            Calendar1.VisibleDate = DateTime.Today;
        }
    }
    FillDayControls(Calendar1.VisibleDate);
}

void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
    if (!DayControls.ContainsKey(e.Day.Date)) return;

    foreach (var control in DayControls[e.Day.Date])
    {
        control.Parent.Controls.Remove(control);

        e.Cell.Controls.Add(control);

        if (control is Button)
        {
            ClientScript.RegisterForEventValidation(control.UniqueID);
        }
    }
}

private void FillDayControls(DateTime dateTime)
{
    var firstMonthDate = new DateTime(dateTime.Year, dateTime.Month, 1);

    var calendarFirstWeekDay = Calendar1.FirstDayOfWeek == FirstDayOfWeek.Default
                                ? CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek
                                : (DayOfWeek) (int) Calendar1.FirstDayOfWeek;

    var currentDate = firstMonthDate.DayOfWeek == calendarFirstWeekDay ? firstMonthDate.AddDays(-7) : firstMonthDate.AddDays((int)firstMonthDate.DayOfWeek * -1);
    var lastDate = currentDate.AddDays(41);

    do
    {
        var events = GetCalendarEvents(currentDate);
        if (events.Count > 0)
        {
            DayControls.Add(currentDate, new List<Control>());
        }

        foreach (var calendarEvent in events)
        {
            var lbl = new Label
                            {
                                BorderColor = System.Drawing.Color.Black,
                                BorderStyle = BorderStyle.Double,
                                BackColor = System.Drawing.Color.BlanchedAlmond,
                                Text = string.Format("{0}<br />{1:hh:mm:ss tt} -- {2:hh:mm:ss tt}", calendarEvent.Title, calendarEvent.Start, calendarEvent.End)
                            };
            DayControls[currentDate].Add(lbl);
            PlaceHolder1.Controls.Add(lbl);

            var btn = new Button
                            {
                                Text = "x",
                                ID = "DeleteEvent_" + calendarEvent.Id,
                                CommandName = "DeleteEvent",
                                CommandArgument = calendarEvent.Id
                            };

            DayControls[currentDate].Add(btn);
            PlaceHolder1.Controls.Add(btn);
            btn.Click += new EventHandler(btn_Click);

        }

        currentDate = currentDate.AddDays(1);
    } while (currentDate <= lastDate);
}

private void btn_Click(object sender, EventArgs e)
{
    var eventId = ((Button)sender).CommandArgument;
}

private List<CalendarEvent> GetCalendarEvents(DateTime dateTime)
{
    return new List<CalendarEvent>{
                    new CalendarEvent
                    {
                        Id = string.Format("{0:MMMddyyyy}Event" ,dateTime),
                        Start = dateTime.Date.AddHours(10),
                        End = dateTime.Date.AddHours(12),
                        Title = string.Format("{0:dd/MM} meeting", dateTime)
                    }};
}

CalendarEvent 类存根:

public class CalendarEvent
{
    public string Id { get; set; }

    public DateTime Start { get; set; }

    public DateTime End { get; set; }

    public string Title { get; set; }
}
于 2012-09-22T17:10:41.253 回答