单击从GridView中的SQL返回日历上所选日期的数据

本文关键字:日期 数据 日历 GridView 中的 SQL 返回 单击 | 更新日期: 2023-09-27 18:01:05

我正在为一家中小型企业创建一个轮调系统,作为我在大学的最后一年项目。我正在尝试创建一个功能,用户可以点击日历,系统将自动返回所选日期的班次。这是代码:

<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>&nbsp;</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>&nbsp;</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) { }
}

单击从GridView中的SQL返回日历上所选日期的数据

检查这个http://msdn.microsoft.com/en-us/library/ms228044%28v=vs.100%29.aspx

我在我的项目中使用过它。

这肯定对你有帮助。

甚至我也想这么做,所以我这样做了:
1-拖放日历控件和网格视图
2-在gridview中添加一个数据源,并指定如下列从tablename中选择column 1,2,3,其中your_date_column=日历的控制。configure数据源将为您提供选择控件的选项
3-并且已经完成:(我的正在工作