日期在PostBack上失去对asp:calendar的关注

时间:2018-10-05 16:26:16

标签: c# asp.net

我有一个ASP:Calendar,在其中可以通过带回发的单选按钮列表控制日期和日历信息。我遇到的问题是当我将日历页面包含在母版页中时,单选按钮失去了焦点,并且没有传递值。当我不使用母版页时,日历工作正常。当我尝试在页面加载中使用!IsPostBack时,单选按钮会传递该值,并且不会失去焦点。但是,日历上没有显示任何数据。

<asp:RadioButtonList CssClass="Space" CellSpacing="5" CellPadding="40" Font-Names="Arial" Font-Size="0.8em" RepeatLayout="Table" ID="valueDate" RepeatDirection="Horizontal" runat="server" AutoPostBack="true" /> 

隐藏代码

protected DataSet dsEvents;

protected void Page_Load(object sender, EventArgs e)
{
        Calendar1.FirstDayOfWeek = FirstDayOfWeek.Sunday;
        Calendar1.NextPrevFormat = NextPrevFormat.FullMonth;
        Calendar1.TitleFormat = TitleFormat.Month;
        Calendar1.ShowGridLines = true;
        Calendar1.DayStyle.HorizontalAlign = HorizontalAlign.Left;
        Calendar1.DayStyle.VerticalAlign = VerticalAlign.Top;
        Calendar1.OtherMonthDayStyle.BackColor = System.Drawing.Color.LightGray;

        if (Request.Form["valueDate"] != null)
        {
            string valueDate = Request.Form["valueDate"];
            Calendar1.VisibleDate = DateTime.Parse(valueDate);
            FillEventDataset();
            GetFirstDayOfNextMonth();
            BindData();
        }
        else
        {
            string valueDate = DateTime.Now.ToShortDateString();
            Calendar1.VisibleDate = DateTime.Parse(valueDate);
            FillEventDataset();
            GetFirstDayOfNextMonth();
            BindData();
        }
}

private void BindData()
{
    string connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;

    using (SqlConnection conn = new SqlConnection(connectionString))
    {
        SqlCommand comm = new SqlCommand("SELECT MonthNum, YearNum, MnthYr, CONVERT(VARCHAR(10), StartOfMonth, 101) AS StartofMonth, CASE WHEN CurrentMonth = StartOfMonth THEN 'True' ELSE 'False' END AS selectedMonth FROM (SELECT DISTINCT RTRIM(Monthname) + ' ' + CAST(YearNum AS varchar(4)) AS MnthYr, Convert(date, DATEADD(month, DATEDIFF(month, 0, StandardDate), 0)) AS StartOfMonth, Convert(date, DATEADD(month, DATEDIFF(month, 0, GetDate()), 0)) AS CurrentMonth, MonthNum, YearNum  FROM CalendarDates WHERE StandardDate BETWEEN DATEADD(month, -3, GETDATE()) AND DATEADD(month, +3, GETDATE())) AS TBL1 Order By YearNum, MonthNum", conn);
        comm.CommandType = CommandType.Text;
        conn.Open();
        SqlDataReader reader = comm.ExecuteReader();
        while (reader.Read())
        {
            ListItem item = new ListItem();
            item.Text = reader["MnthYr"].ToString();
            item.Value = reader["StartofMonth"].ToString();
            item.Selected = Convert.ToBoolean(reader["selectedMonth"]);
            valueDate.Items.Add(item);
        }
        reader.Close();
        conn.Close();
    }
}

protected DateTime GetFirstDayOfNextMonth()
{
    int monthNumber, yearNumber;
    if (Calendar1.VisibleDate.Month == 12)
    {
        monthNumber = 1;
        yearNumber = Calendar1.VisibleDate.Year + 1;
    }
    else
    {
        monthNumber = Calendar1.VisibleDate.Month + 1;
        yearNumber = Calendar1.VisibleDate.Year;
    }
    DateTime lastDate = new DateTime(yearNumber, monthNumber, 1);
    return lastDate;
}

protected void FillEventDataset()
{
    DateTime firstDate = new DateTime(Calendar1.VisibleDate.Year, Calendar1.VisibleDate.Month, 1);
    DateTime lastDate = GetFirstDayOfNextMonth();
    dsEvents = GetSelectedMonthData(firstDate, lastDate);
}

protected DataSet GetSelectedMonthData(DateTime firstDate, DateTime lastDate)
{
    DataSet dsEvents = new DataSet();

    string connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;

    using (SqlConnection conn = new SqlConnection(connectionString))
    {
        SqlCommand comm = new SqlCommand("SELECT EventDate, EventLocation, EventSubject, EventStart FROM EventList WHERE EventDate >= @FirstDate AND EventDate < @LastDate", conn);

        comm.CommandType = CommandType.Text;
        comm.Parameters.Add("@FirstDate", SqlDbType.DateTime).Value = firstDate;
        comm.Parameters.Add("@LastDate", SqlDbType.DateTime).Value = lastDate;

        conn.Open();
        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(comm);
        try
        {
            sqlDataAdapter.Fill(dsEvents);
        }
        finally
        {
            conn.Close();
        }

    }
    return dsEvents;
}

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
    e.Day.IsSelectable = false;
    e.Cell.ForeColor = System.Drawing.Color.Black;
    if (e.Day.IsOtherMonth)
    {
        e.Cell.Visible = true;
        e.Cell.Text = "";
    }

    DateTime nextEvent;
    String nextLocation;
    String nextSubject;
    String nextStart;

    if (dsEvents != null)
    {
        foreach (DataRow dr in dsEvents.Tables[0].Rows)
        {

            nextEvent = (DateTime)dr["EventDate"];
            nextLocation = dr["EventLocation"].ToString();
            nextSubject = dr["EventSubject"].ToString();
            nextStart = dr["EventStart"].ToString();

            if (nextEvent == e.Day.Date)
            {
                Literal literal1 = new Literal();
                literal1.Text = "<br/>";
                e.Cell.Controls.Add(literal1);

                Label label1 = new Label();
                label1.Text = nextStart.ToString();
                label1.Font.Size = new FontUnit(FontSize.Small);
                e.Cell.Controls.Add(label1);

                Literal literal2 = new Literal();
                literal2.Text = "&nbsp;&nbsp;";
                e.Cell.Controls.Add(literal2);

                Label label2 = new Label();
                label2.Text = nextSubject.ToString();
                label2.Font.Size = new FontUnit(FontSize.Small);
                e.Cell.Controls.Add(label2);

                Literal literal3 = new Literal();
                literal3.Text = "&nbsp;&nbsp;";
                e.Cell.Controls.Add(literal3);

                Label label3 = new Label();
                label3.Text = nextLocation.ToString();
                label3.Font.Size = new FontUnit(FontSize.Small);
                e.Cell.Controls.Add(label3);
            }

        }
    }
}

0 个答案:

没有答案