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;
}
}
2
Answers
Inside the
else
part you can set thedate
any particular reason you don’t just drop in a plane jane textbox, and set it to as date?
Like this:
and thus we get this:
so, with above, the text box is still the text box, but also is the datepicker.
Edit#2: Select date on one page, pass to next page.
Ok, so we going to select a date on one page. Jump to next page.
On next page, we have a text box to select the date (or pass from previous page).
Also, when we update/change this text box, we want to re-load the grid data based on this text box.
So, first page we have this:
Text box, and button to jump to next page
our button click code for above button is this:
We now jump to page 2, and on first load (!IsPostback), then we have this code:
A date box above the grid (to filter),
And gridview.
and the code behind is this:
the result looks like this:
So, I only have/need one text box. and I set autopostback = true.
So, the code for that text box is thus:
So, whatever value you shove into that text box should persit, should survice any post-back on that page, and it should retain its value.
Of course, you cannot build a working asp.net page UNLESS your setup code for that page is in that ALL VERY important !IsPostBack code stub.
The last 200+ web pages I build ALL HAVE that !IsPostBack stub, and you cannot really quite build a working web page unless you include that code stub.
if you do not check if this is REALLY the first page load, then all of your setup code for the page will run again and again – overwriting any changes you might make to such pages.
the above code – tested and works just fine.
So, you should not have any issues setting the value of the text box on that current page, or even passing a value for the text box from the previous page.
However, you do need to ensure that you don’t re-set the text box value on each page load – and hence the !IsPostBack stub.