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

问题描述:

我有下面的代码。当我在特定的日子cell.the添加数据插入另一daycell.please help.what是在我的代码中的问题有时。为什么数据被正确地插入NAD有时它被插入在一些其它天细胞 例如: 如果我在23/2 插入 19/3如何在日历控件中插入编辑数据日期渲染事件asp.net

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 
+0

请帮忙编辑选项也 – 2015-03-25 12:01:07

+0

使用convert.ToDate()或DateTime.TryParse()将hdnSelectedDate转换为适当的日期格式,然后再添加SQL。 – Kami 2015-03-25 12:48:03

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