使用ASP.NET在日历中添加事件

时间:2015-04-17 12:53:41

标签: asp.net

我有一个像here

这样的代码

我只接受了添加任务窗格的部分 default.aspx中的代码就像这样

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script>
        function ShowAddTaskPane(e, selectedDate) {
            var ev = e || window.event;
            document.getElementById("AddTaskPane").style.visibility = 'visible';
            document.getElementById("AddTaskPane").style.top = ev.clientY;
            document.getElementById("AddTaskPane").style.left = ev.clientX;
            CallServer(selectedDate, '');
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Calendar ID="Calendar1" runat="server" Height="600px" OnSelectionChanged="Calendar1_SelectionChanged" Width="1000px"></asp:Calendar>
    </div>
        <div id="AddTaskPane"  style="position:absolute; visibility:hidden; width:150px; height:100px; background-color:#FFFF66; top: 87px; left: 113px;">
       Enter Title:<asp:TextBox ID="txtTitle" runat="server" />
           <asp:Button ID="Btn_AddTask" runat="server" Text="Add Task" OnCommand="Btn_AddTask_Command" />     
       </div>
    </form>
</body>
</html>

Default.aspx.cs中的代码就像这样

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Btn_AddTask_Command(object sender, CommandEventArgs e)
    {

    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {

    }

    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        HtmlAnchor anchor = new HtmlAnchor();

        anchor.InnerHtml = "Add";



        string method = "ShowAddTaskPane(event,'" + e.Day.Date.ToShortDateString() + "')";
        anchor.HRef = "#";
        anchor.Attributes.Add("onclick", method);
        e.Cell.Controls.Add(anchor);
    }



}

我的问题是:为什么在选择日期时没有出现addtaskpane函数?

1 个答案:

答案 0 :(得分:0)

您需要将事件处理程序方法与Calendar控件的相应事件连接起来,如下所示: -

<asp:Calendar ID="Calendar1" runat="server" Height="600px" 
OnSelectionChanged="Calendar1_SelectionChanged" OnDayRender="Calendar1_DayRender" 
Width="1000px"></asp:Calendar>