skip to Main Content

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


  1. Inside the else part you can set the date

    if (Calendar1.Visible)
    {
        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);
        }
    }
    
    Login or Signup to reply.
  2. 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.

    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

            <h4>Select date</h4>
            <asp:TextBox ID="TextBox1" runat="server"
                TextMode="date"
                >
            </asp:TextBox>
            <br />
            <asp:Button ID="Button1" runat="server" Text="Submit"
                cssClass="btn" OnClick="Button1_Click1"/>
    

    our button click code for above button is this:

        protected void Button1_Click1(object sender, EventArgs e)
        {
            Session["MyDate"] = TextBox1.Text;
            Response.Redirect("WebForm2.aspx");
        }
    

    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.

           <lable style="font-size:large">Bookings for</lable>
            <asp:TextBox ID="TextBox1" runat="server"
                textmode="Date" AutoPostBack="true"
                OnTextChanged="TextBox1_TextChanged" Font-Size="Large"
                ></asp:TextBox>
    
            <br />
            <br />
            <asp:GridView ID="GridView1" runat="server" CssClass="table" Width="40%" >
    
            </asp:GridView>
    

    and the code behind is this:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TextBox1.Text = (string)Session["MyDate"];
                if (TextBox1.Text == "")
                    TextBox1.Text = DateTime.Today.ToString("yyyy-MM-dd");
                LoadGrid();
            }
        }
    
        void LoadGrid()
        {
            string strSQL =
                @"SELECT * FROM vBookings WHERE @From = cast(FromDate as date)
                ORDER BY FromDate";
            using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
            {
                using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
                {
                    conn.Open();
                    cmdSQL.Parameters.Add("@From", SqlDbType.DateTime).Value = TextBox1.Text;
                    DataTable rstData = new DataTable();
                    rstData.Load(cmdSQL.ExecuteReader());
                    GridView1.DataSource = rstData;
                    GridView1.DataBind();
                }
            }
        }
    

    the result looks like this:

    enter image description here

    So, I only have/need one text box. and I set autopostback = true.

    So, the code for that text box is thus:

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            LoadGrid();
        }
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search