我正在为一家中小型企业创建一个轮换系统,作为我在大学的最后一年项目。我正在尝试创建一个功能,用户可以在其中单击日历,系统将自动返回所选日期的班次。这是代码:
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Select date to show rota for selected date:
</h2>
<p>
<asp:Calendar ID="Calendar" runat="server" nextprevformat="shortmonth" OnSelectionChanged="Calendar_SelectionChanged" />
<br />
Date Selected: <asp:TextBox id="textSelected" runat="server" Text="2013-02-21" AutoPostBack="true"></asp:TextBox>
</p>
<asp:Label id="lbl" runat="server"></asp:Label>
<p> </p>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Shift_ID" DataSourceID="SqlDataSource1" onselectedindexchanged="GridView1_SelectedIndexChanged" AllowSorting="True">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="Shift_ID" HeaderText="Shift_ID" ReadOnly="True" SortExpression="Shift_ID" />
<asp:BoundField DataField="Shift_Date" HeaderText="Shift_Date" SortExpression="Shift_Date" />
<asp:BoundField DataField="Start_Time" HeaderText="Start_Time" SortExpression="Start_Time" />
<asp:BoundField DataField="End_Time" HeaderText="End_Time" SortExpression="End_Time" />
<asp:BoundField DataField="Employee_ID" HeaderText="Employee_ID" SortExpression="Employee_ID" />
<asp:BoundField DataField="Shift_Duration" HeaderText="Shift_Duration" SortExpression="Shift_Duration" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:rotasystemConnectionString %>" SelectCommand="SELECT [Shift_ID], [Shift_Date], [Start_Time], [End_Time], [Employee_ID], [Shift_Duration] FROM [Shift]">
</asp:SqlDataSource>
<p> </p>
</asp:Content>
和
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class CalendarSelect : System.Web.UI.Page
{
public string vconnstr;
public string strsql;
protected void Calendar_SelectionChanged(object o, EventArgs e)
{
textSelected.Text = Calendar.SelectedDate.ToString("yyyy-MM-dd");
if (Page.IsPostBack == true)
{
lbl.Text = "This needs to return a gridview showing the shifts for the date of:";
lbl.Text += "<br /> " + Calendar.SelectedDate.ToShortDateString();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { }
}