积极答复者
Calendar控件的SelectionChanged事件无效

问题
-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script type="text/javascript" src="jquery-2.1.1.min.js"></script> <script type="text/javascript"> $(function () { $("#Calendar1,#Calendar2").css("display", "none"); //页面一加载隐藏时间控件 //入住时间 //获取光标时,显示时间控件 $("#TextBox1").focus(function () { $("#Calendar1").css("display", "block"); }); //失去光标时隐藏控件 $("#TextBox1").blur(function () { $("#Calendar1").css("display", "none"); }); }) </script> </head> <body> <form id="form1" runat="server"> <div> <br /> <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" ></asp:TextBox> <br /> <asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66" BorderWidth="1px" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399" Height="150px" ShowGridLines="True" Width="150px"> <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" /> <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" /> <OtherMonthDayStyle ForeColor="#CC9966" /> <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" /> <SelectorStyle BackColor="#FFCC66" /> <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" /> <TodayDayStyle BackColor="#FFCC66" ForeColor="White" /> </asp:Calendar> <br /> <br /> <asp:Button ID="Button1" runat="server" Text="确定" /> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Calendar1_SelectionChanged(object sender, EventArgs e) { TextBox1.Text = Calendar1.SelectedDate.ToString(); } }
上面的代码中,我想选取控件中的日期,然后把选取的日期添加到文本框中,但不知道为什么calendar控件的SelectionChanged事件无效,各位大神帮忙给看看怎么回事,多谢了!!
- 已移动 ThankfulHeart 2014年8月19日 4:49 asp.net问题
答案
-
你好,
你的Calendar控件,在页面中没有绑定OnSelectionChanged事件。然后你应该移除text的blur事件。
请尝试下面的代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Calendar2.aspx.cs" Inherits="GriviewDemo2.Calender.Calendar2" %> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script src="../Scripts/jquery-1.10.2.js"></script> <script type="text/javascript"> $(function () { $("#Calendar1,#Calendar2").css("display", "none"); //页面一加载隐藏时间控件 //入住时间 //获取光标时,显示时间控件 $("#TextBox1").focus(function () { $("#Calendar1").css("display", "block"); }); //失去光标时隐藏控件 //$("#TextBox1").blur(function () { // $("#Calendar1").css("display", "none"); //}); }) </script> </head> <body> <form id="form1" runat="server"> <div> <br /> <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" ></asp:TextBox> <br /> <asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66" BorderWidth="1px"
DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399" Height="150px"
ShowGridLines="True" Width="150px" OnSelectionChanged="Calendar1_SelectionChanged"> <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" /> <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" /> <OtherMonthDayStyle ForeColor="#CC9966" /> <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" /> <SelectorStyle BackColor="#FFCC66" /> <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" /> <TodayDayStyle BackColor="#FFCC66" ForeColor="White" /> </asp:Calendar> <br /> <br /> <asp:Button ID="Button1" runat="server" Text="确定" /> </div> </form> </body> </html>
Best Regards,
Kevin Shen.
- 已编辑 Kevin Shen 2014年8月21日 3:21
- 已标记为答案 qunge 2014年8月28日 5:43
全部回复
-
你好,
你的Calendar控件,在页面中没有绑定OnSelectionChanged事件。然后你应该移除text的blur事件。
请尝试下面的代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Calendar2.aspx.cs" Inherits="GriviewDemo2.Calender.Calendar2" %> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script src="../Scripts/jquery-1.10.2.js"></script> <script type="text/javascript"> $(function () { $("#Calendar1,#Calendar2").css("display", "none"); //页面一加载隐藏时间控件 //入住时间 //获取光标时,显示时间控件 $("#TextBox1").focus(function () { $("#Calendar1").css("display", "block"); }); //失去光标时隐藏控件 //$("#TextBox1").blur(function () { // $("#Calendar1").css("display", "none"); //}); }) </script> </head> <body> <form id="form1" runat="server"> <div> <br /> <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" ></asp:TextBox> <br /> <asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66" BorderWidth="1px"
DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399" Height="150px"
ShowGridLines="True" Width="150px" OnSelectionChanged="Calendar1_SelectionChanged"> <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" /> <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" /> <OtherMonthDayStyle ForeColor="#CC9966" /> <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" /> <SelectorStyle BackColor="#FFCC66" /> <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" /> <TodayDayStyle BackColor="#FFCC66" ForeColor="White" /> </asp:Calendar> <br /> <br /> <asp:Button ID="Button1" runat="server" Text="确定" /> </div> </form> </body> </html>
Best Regards,
Kevin Shen.
- 已编辑 Kevin Shen 2014年8月21日 3:21
- 已标记为答案 qunge 2014年8月28日 5:43