如何在日历控件天呈现事件asp.net中插入编辑数据



我有下面的代码。当我在特定的日期单元格中添加数据时。数据插入另一个日期单元格。请帮助。我的代码有什么问题。为什么有时数据插入正确,有时它插入其他日期单元格例如:如果我在2月23日插入日期插入的数据19/3

    private DataSet GetData()
        {
        string strcon = ConfigurationManager.ConnectionStrings["ConnectionCommon"].ConnectionString;
            SqlConnection con = new SqlConnection(strcon);
            con.Open();
            SqlDataAdapter ad = new SqlDataAdapter("PrcScheduler", con);
            DataSet ds = new DataSet();
            ad.Fill(ds);
            con.Close();
           return ds;
        }
        //code to insert the data in table
        protected void Btn_AddTask_Click(object sender, EventArgs e)
        {
            string strcon = ConfigurationManager.ConnectionStrings["ConnectionCommon"].ConnectionString;
            SqlConnection con = new SqlConnection(strcon);
            con.Open();
            SqlCommand cmd = new SqlCommand("prcinsertscheduler", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Schedule_title", txtTitle.Text.ToString());
            cmd.Parameters.AddWithValue("@Schedule_Date", hdnSelectedDate.Value);
            cmd.ExecuteNonQuery();
            con.Close();
            txtTitle.Text = "";
        }
        //dayrender event
        protected void calendar12_DayRender(object sender, DayRenderEventArgs e)
        {
            DataSet ds = GetData();
            string link = "<a href=' Calendar.aspx?Schedule_ID=";
            string s = e.Day.Date.ToShortDateString();
            e.Cell.Text = e.Day.Date.Day.ToString() + "<BR>";
            LiteralControl l = new LiteralControl();
            l.Text = e.Day.Date.Day.ToString() + "<BR>";
            e.Cell.Controls.Add(l);
      //retriving the data in calenar cel
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                string scheduledDate = Convert.ToDateTime(row["Schedule_Date"]).ToShortDateString();
                if (scheduledDate.Equals(s))
                {
                    LinkButton lb = new LinkButton();
                    lb.Text = link + (int)row["Schedule_ID"] + "'>" + row["Schedule_title"] as String + "</a>" + "<BR>";
                    e.Cell.Controls.Add(lb);
                }
            }

            HtmlAnchor anchor = new HtmlAnchor();
            anchor.InnerHtml = "Add";
            string method = "ShowAddTaskPane(event,'" + e.Day.Date.ToShortDateString() + "')";
            anchor.HRef = "#";
            anchor.Attributes.Add("onclick", method);
            //To add the htmlanchor in the panel and show that on mouseover
            Panel p1 = new Panel();
            p1.ID = "p" + e.Day.DayNumberText + e.Day.Date.Month.ToString(); ;
            p1.Attributes.Add("style", "display:none;");
            p1.Controls.Add(anchor); ;
            e.Cell.Controls.Add(p1);
            e.Cell.Attributes.Add("onmouseover", "ShowInfo('" + p1.ClientID + "')");
            e.Cell.Attributes.Add("onmouseout", "HideInfo('" + p1.ClientID + "')");
        }
        protected void btncancel_Click(object sender, EventArgs e)
        {
            calendar12.Visible = true;
        }
    }
}

.aspx

    <title></title>
    <script type="text/javascript">
        function ShowAddTaskPane(e, selectedDate) {
            debugger;
            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;
        }
        //    function trimByWord(sentence) {
        //    var result = sentence;
        //    var resultArray = result.split(” “);
        //    if(resultArray.length > 10){
        //    resultArray = resultArray.slice(0, 10);
        //    result = resultArray.join(” “) + “…”;
        //    }
        //return result;
        //}
        function ShowInfo(id) {
            var div = document.getElementById(id);
            document.getElementById("hdnSelectedDate").value = div.innerHTML.match(/'([^']+)'/)[1];
            div.style.display = "block";
        }
        function HideInfo(id) {
            var div = document.getElementById(id);
            div.style.display = "none";
        }

    </script>    
        <table>
            <tr>
                <td>
                    <asp:TextBox ID="txtTitle" runat="server" TextMode="MultiLine" Rows="5" />
                </td>
       </tr>
            <tr>
                <td>
                    <asp:Button ID="Btn_AddTask" runat="server" Text="Save" Width="44px" OnClick="Btn_AddTask_Click" />
                    <asp:Button ID="btncancel" runat="server" Text="Cancel" 
                        onclick="btncancel_Click" />
                    <asp:Button ID="btnoption" runat="server" Text="Option" />
                </td>
            </tr>
        </table>
    </div>
    <div>
    <table>
    <tr>
    <td>
        <asp:Calendar ID="calendar12" runat="server" ondayrender="calendar12_DayRender" 
            BackColor="White" BorderColor="Black" BorderStyle="Solid" CellSpacing="1" 
            Font-Names="Verdana" Font-Size="9pt" ForeColor="Black" Height="291px" 
            NextPrevFormat="ShortMonth" Width="416px">
            <DayHeaderStyle Font-Bold="True" Font-Size="8pt" ForeColor="#333333" 
                Height="8pt" />
            <DayStyle BackColor="#CCCCCC" />
            <NextPrevStyle Font-Bold="True" Font-Size="8pt" ForeColor="White" />
            <OtherMonthDayStyle ForeColor="#999999" />
            <SelectedDayStyle BackColor="#333399" ForeColor="White" />
            <TitleStyle BackColor="#333399" BorderStyle="Solid" Font-Bold="True" 
                Font-Size="12pt" ForeColor="White" Height="12pt" />
            <TodayDayStyle BackColor="#999999" ForeColor="White" />
        </asp:Calendar>
          <asp:HiddenField ID="hdnSelectedDate" runat="server" /> 
    </td>
    </tr>
    </table>
      </div>
    </form>
</body>
</ht

日期格式可能有问题。您的hdnSelectedDate.Value是一个文本,需要使用正确的格式。它的输入由ShowInfo生成,您需要确保输入正确,并且Value已转换为有效的日期表示。

最新更新