Home > Enterprise >  Setting StartDate for calendar from textbox
Setting StartDate for calendar from textbox

Time:09-09

By default the calendar tool always opens to the current date when first clicked. Any clicks after that, opens it to the date previously clicked.

Is there a way to have the initial click open on a date that depends on a textbox?

Say for example: Upon loading the page, a textBox already has the date 2011/05/15. When the user clicks the calendar image button to open the calendar, the year, month and day are already set to 2011/05/15.

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadGV();
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {

            if (Calendar1.Visible)
            {
                txtTime.Text = Calendar1.SelectedDate.ToString("yyyy/MM/dd");
                Calendar1.Visible = false;
            }
            else
            {
                Calendar1.Visible = true;
                Calendar1.Attributes.Add("style", "BACKGROUND: white; POSITION: absolute");
                Calendar1.SelectedDate = DateTime.Parse(txtTime.Text);
            }
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            Session["TIME"] = Calendar1.SelectedDate.ToString("yyyy/MM/dd");
            txtTime.Text = Calendar1.SelectedDate.ToString("yyyy/MM/dd");
            Calendar1.Visible = false;
        }

        protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {
            if (e.Day.IsOtherMonth)
            {
                e.Day.IsSelectable = false;
            }
        }

CodePudding user response:

Inside the else part you can set the date

if (Calendar1.Visible)
{
    txtTime.Text = Calendar1.SelectedDate.ToString("yyyy/MM/dd");
    Calendar1.Visible = false;
}
else
{
    Calendar1.Visible = true;
    Calendar1.Attributes.Add("style", "BACKGROUND: white; POSITION: absolute");
    if(!string.IsNullOrEmpty(txtTime.Text))
    {
        Calendar1.SelectedDate = DateTime.Parse(txtTime.Text);
    }
}

CodePudding user response:

any particular reason you don't just drop in a plane jane textbox, and set it to as date?

Like this:

    <h4>Enter Date</h4>
    <asp:TextBox ID="txtDate" runat="server" TextMode="Date">
    </asp:TextBox>

and thus we get this:

enter image description here

so, with above, the text box is still the text box, but also is the datepicker.

  • Related