我有下面的代码。当我在特定日期单元格中添加数据时。数据插入另一个daycell.please帮助。我的代码中的问题是什么。为什么有时数据被正确插入nad有时它被插入其他一些细胞 例如: 如果我在23/2插入日期 插入的数据 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
答案 0 :(得分:0)
日期格式化可能存在问题。您的hdnSelectedDate.Value
是一个文本,需要采用正确的格式。它的输入由ShowInfo
生成,您需要确保输入都正确并且Value
转换为有效的日期表示。