日历之间的冲突

时间:2017-10-20 22:48:22

标签: c# asp.net date datetime

我有两个这样的约会拣货员:

enter image description here

HTML

      <asp:TextBox ID="fecha_ini" ClientIDMode="Static" runat="server"></asp:TextBox>
        <asp:Calendar ID="Calendar2" runat="server" OnSelectionChanged="DateChange" DayNameFormat="FirstLetter" Font-Names="Tahoma" Font-Size="11px" NextMonthText="." PrevMonthText="." SelectMonthText="»" SelectWeekText="›" CssClass="myCalendar" CellPadding="0">
            <OtherMonthDayStyle ForeColor="#b0b0b0" />
            <DayStyle CssClass="myCalendarDay" ForeColor="#2d3338" />
            <DayHeaderStyle CssClass="myCalendarDayHeader" ForeColor="#2d3338" />
            <SelectedDayStyle Font-Bold="True" Font-Size="12px" CssClass="myCalendarSelector" />
            <TodayDayStyle CssClass="myCalendarToday" />
            <SelectorStyle CssClass="myCalendarSelector" />
            <NextPrevStyle CssClass="myCalendarNextPrev" />
            <TitleStyle CssClass="myCalendarTitle" />
        </asp:Calendar>
    </td>
 <td>
                <asp:TextBox ID="fecha_fin" ClientIDMode="Static" runat="server"></asp:TextBox>
                <asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="DateChange" DayNameFormat="FirstLetter" Font-Names="Tahoma" Font-Size="11px" NextMonthText="." PrevMonthText="." SelectMonthText="»" SelectWeekText="›" CssClass="myCalendar" CellPadding="0">
                    <OtherMonthDayStyle ForeColor="#b0b0b0" />
                    <DayStyle CssClass="myCalendarDay" ForeColor="#2d3338" />
                    <DayHeaderStyle CssClass="myCalendarDayHeader" ForeColor="#2d3338" />
                    <SelectedDayStyle Font-Bold="True" Font-Size="12px" CssClass="myCalendarSelector" />
                    <TodayDayStyle CssClass="myCalendarToday" />
                    <SelectorStyle CssClass="myCalendarSelector" />
                    <NextPrevStyle CssClass="myCalendarNextPrev" />
                    <TitleStyle CssClass="myCalendarTitle" />
                </asp:Calendar>

            </td>

.CS控制器

 protected void Page_Load(object sender, EventArgs e)
     {
 fecha_ini.Text = DateTime.Today.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
 fecha_fin.Text = DateTime.Today.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
                    Thread.CurrentThread.CurrentCulture = new CultureInfo("es-MX");
     }

 protected void DateChange(object sender, EventArgs e)
        {
            fecha_fin.Text = Calendar1.SelectedDate.ToString("dd/MM/yyyy");
            fecha_ini.Text = Calendar2.SelectedDate.ToString("dd/MM/yyyy");

        }

问题是当我在第二个日历中选择日期时,它会正确更改它但是将第一个日历日期刷新回今天,我该如何解决?此致

2 个答案:

答案 0 :(得分:1)

您需要在IsPostBack项检查中包装TextBoxes的初始设置。如果不是,每次有回发时都会重置它们。

protected void Page_Load(object sender, EventArgs e)
{
    Thread.CurrentThread.CurrentCulture = new CultureInfo("es-MX");

    if (!IsPostBack)
    {
        fecha_ini.Text = DateTime.Today.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
        fecha_fin.Text = DateTime.Today.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
    }
}

答案 1 :(得分:0)

页面回发时会触发您的页面加载事件。尝试将语句包装在if语句中,以便它只在页面加载时执行。

if (!IsPostBack)
   {
      fecha_fin.Text = Calendar1.SelectedDate.ToString("dd/MM/yyyy");
      fecha_ini.Text = Calendar2.SelectedDate.ToString("dd/MM/yyyy"); 

    }