Get value of dynamically created RadDatePicker

5 posts, 0 answers
  1. Philip Senechal
    Philip Senechal avatar
    152 posts
    Member since:
    Apr 2008

    Posted 23 Jun 2011 Link to this post

    Could use a little assistance here...

    I have a RadComboBox that when changed to a certain value, does a postback and dynamically creates a RadDatePicker server side inside a Panel control and adds it to a Div on the page. Once that is done, I have an ASP:Button that when clicked, needs to get the SelectedDate value of that RadDatePicker.

    I believe from what I've read, the problem is that dynamically created controls aren't available when the ASP:Button does its postback. In the button event, I'm recreating the Panel and RadDatePicker so that I can reference the controls, but once I do that, the SelectedDate value of the RadDatePicker is gone.

    Can you possibly show me how this can be done so that I can dynamically create a RadDatePicker and then retrieve it's value during postback when a button is clicked? Please note that if I turn on AutoPostBack=true, I lose the dynamically created RadDatePicker on postback.

    Thanks so much!
  2. Cori
    Cori avatar
    562 posts
    Member since:
    Jul 2010

    Posted 24 Jun 2011 Link to this post

    Hello Philip,

    You need to re-create the RadDatePicker control in the Page_Load event, not the button Click because the ViewState is loaded after the Page_Load event, but before the button's Click event. Thus the reason you can't get the SelectedDate in the click event because the ViewState has already been loaded for all the controls.

    I hope that helps.
  3. Genti
    Admin
    Genti avatar
    107 posts

    Posted 24 Jun 2011 Link to this post

    Hi Philip Senechal,

    It is true, you are going to lose the DatePicker control aftert the first postback.
    In order to keep the picker control alive and able to maintain its ViewState you would have to do the following.
    Make sure you instantiate the control in the Init phase of the page lifecycle. This way you won't miss the ViewState loading, and your control would be able to maintain its state. Also, make sure you instantiate the control with the same id that you used when you instatiated it in the button click.
    i.e
    protected void Page_Init(object sender, EventArgs e)
    {
        if (Session["pickerLoaded"] != null && (bool)Session["pickerLoaded"])
        {
            RadDatePicker myPicker = new RadDatePicker();
            myPicker.ID = "RadDatePicker1";
     
            Panel1.Controls.Add(myPicker);
        }
    }

    And in the button click event you would have:
    public void ValueChanged(object sender, EventArgs e)
    {
        RadDatePicker  myPicker = new RadDatePicker();
        myPicker.ID = "RadDatePicker1";
        Session["pickerLoaded"] = true;
     
        Panel1.Controls.Add(myPicker);
    }

    I am also attaching a simple runnable solution.
    Hope that this helps.


    Best wishes,
    Genti
    the Telerik team

    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

  4. Philip Senechal
    Philip Senechal avatar
    152 posts
    Member since:
    Apr 2008

    Posted 11 Jul 2011 Link to this post

    Hi Genti,

    Thanks so much for your assistance. I'm almost there with this piece and it took me a while to get things working properly with my code.

    Your solution helped tremendously in getting the values from those dynamically created controls...I can now get the values. The solution created another issue though that I'm not sure how to correct. Let me describe the dynamic controls and how they work on this page.

    The first dynamic control that determines whether the next 2 dynamic controls are displayed is Date Period. This dynamic ComboBox has several values, including Current Month and Year to Date, which do not cause additional dynamic controls to be displayed. It also has a value Specific Dates which then causes the dynamic datepickers to appear.

    So...all of this works great except for one thing. If I select Specific Dates, my datepickers appear as they should. If I then change that value to Current Month, the datepickers still render on the page until I change the selection again to another value where they shouldn't render such as Year to Date.

    Do you know how I can configure this so that the datepickers are not rendered the first time I chose a value other than Specific Dates? Thanks so much for your help!
  5. Genti
    Admin
    Genti avatar
    107 posts

    Posted 12 Jul 2011 Link to this post

    Hi Philip Senechal,

    I am sending you a modified version of the project I sent you in the previous post, that illustrates how to achieve your goal.
    I am still using the Session variable to store some information (if the check box values are changed or not).

    Let me know if this helps.

    Best wishes,
    Genti
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Back to Top