C# 如何在日历控件天渲染事件asp.net中插入编辑数据

C# 如何在日历控件天渲染事件asp.net中插入编辑数据,c#,asp.net,.net,C#,Asp.net,.net,我有下面的代码。当我在特定的日单元格中添加数据时,数据会插入另一个日单元格。请帮助。我的代码中有什么问题。为什么有时数据会正确插入,有时数据会插入另一个日单元格 如: 如果我在23/2中插入日期 插入的数据 19/3 .aspx 这可能是日期格式的问题。您的hdnSelectedDate.Value是文本,需要采用正确的格式。它的输入是由ShowInfo生成的,您需要确保输入正确,并且该值已转换为有效的日期表示形式。请使用编辑选项alsoUse convert.ToDate或DateTime.T

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

.aspx


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

请使用编辑选项alsoUse convert.ToDate或DateTime.TRIPARSE进行帮助,以便在将HDSELECTEDATE添加到SQL之前将其转换为正确的日期格式。
    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;
        }
    }
}
    <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