Search code examples
c#asp.netrepeater

c# use Calendar.SelectedDate in a repeater control, but month is giving me "00"


I am trying to make a webform using asp.net. When a linked button is clicked (within a repeater), a calendar is supposed to popup. The date selected on the calendar is supposed to fill out the text box "start date".

This is one item inside Repeater1:

<td>
      <asp:TextBox runat="server" ID="txtStart" Text='<%# DataBinder.Eval(Container.DataItem,"StartDate") %>' />
      <asp:LinkButton ID="btnCalendar" runat="server" 
      CssClass="btn btn-xs"OnClick="ShowCalendar">
      <span aria-hidden="true" class="glyphicon glyphicon-calendar"></span>
      </asp:LinkButton>
</td>

After all other items, here is the Calendar:

 <asp:Calendar ID="Calendar1" runat="server" Visibile="false"  SelectionMode="Day" onselectionchanged="Calendar1_SelectionChanged">
                  </asp:Calendar>

Code behind:

protected void Calendar1_SelectionChanged(object sender, EventArgs e){
    if (Repeater1.Items.Count == 1)
    {
        TextBox txtStart = (TextBox)Repeater1.Items[0].FindControl("txtStart");
        DateTimeFormatInfo usDtfi = new CultureInfo("en-US", false).DateTimeFormat;

        txtStart.Text = Calendar1.SelectedDate.ToString("yyyymmdd");
    }

This code behind is just adding text to the fist item of the repeater

My problem is:

The SelectedDate of Calendar is giving me the correct year and date, but is giving me value "00" for the month

This is the link to a screenshot of the part of my webform which is problematic

Thank you!


Solution

  • Just change the line

     txtStart.Text = Calendar1.SelectedDate.ToString("yyyymmdd");
    

    with

     txtStart.Text = Calendar1.SelectedDate.ToString("yyyyMMdd");