This is a migrated thread and some comments may be shown as answers.

Dynamically created RadDatePicker not posting back initial data

5 Answers 167 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Matt
Top achievements
Rank 1
Matt asked on 30 Nov 2011, 10:02 PM
I'm trying to get a RadDatePicker to properly postback on my RadGrid.  I'm creating the object dynamically since depending on the row type, I want different types of controls in the column.

To start, inside ItemCreated, I'm doing this to create the control:
DateTime tmpDate;
item["Column2"].Controls.Add( new RadDatePicker() {
    ID = "phDate",
    SelectedDate = DateTime.TryParse( indExpDate, out tmpDate ) ? (DateTime?)tmpDate : null
} );
In this code, indExpDate is set to either my data (if the item.DataItem is available) or to "" (if not).

This is the same code that I'm using to create my TextBox objects as well, which are working fine.

The control is properly created initially, showing the date that I want.  When I post back in an ItemCommand, the SelectedDate is always NULL, and when the page redraws following the postback, the field is blank.  This only happens if I don't change the control value - once I change the control value, all is well.

As I said, TextBox works fine.  I create the control the same way, set the .Text property, can read the data during ItemCommand, and the value comes back when the postback completes.  RadDatePicker is the only one that I'm having trouble with.

Any ideas?

Thanks - Matt

5 Answers, 1 is accepted

Sort by
0
Pablo
Top achievements
Rank 1
answered on 30 Nov 2011, 10:21 PM
Matt, that seems like a problem with the viewstate to me for the raddatetime picker. Have you tried another telerik control besides the textbox? And change its value before postback?
0
Matt
Top achievements
Rank 1
answered on 01 Dec 2011, 02:05 PM
I added the following code right next to the RadDatePicker code that I posted yesterday, and it works properly.

item["Column2"].Controls.Add( new RadTextBox()
{
    ID = "phTest",
    Text = indExpDate
} );

The Text field is properly populated inside ItemCommand, and the value that was initialized is also shown on the page after the postback completes.

Any other ideas?

Matt
0
Pablo
Top achievements
Rank 1
answered on 01 Dec 2011, 06:10 PM
Matt,

What happens is that the "onitemcreated" event is called everytime you postback BEFORE the " onitemcommand" event. So, let's say the date is properly displayed the first time, then you change it. You click for instance "Edit" (to generate the postback) and you want to run some custom code in the "onitemcommand". However, when you click "Edit" it will first run the code in the  "onitemcreated" event which is the following  (before actually running your "onitemcommand" code):

DateTime tmpDate;
item["Column2"].Controls.Add( new RadDatePicker() {
    ID = "phDate",
    SelectedDate = DateTime.TryParse( indExpDate, out tmpDate ) ? (DateTime?)tmpDate : null
} );


Thus, what you have to do is preventing the  "onitemcreated" to be run twice so that it doesn't change what the user has set as the value of the control. You could do the following on the  "onitemcreated":

if(item.FindControl("phDate") == null)
{
  
DateTime tmpDate;
item["Column2"].Controls.Add( new RadDatePicker() {
    ID = "phDate",
    SelectedDate = DateTime.TryParse( indExpDate, out tmpDate ) ? (DateTime?)tmpDate : null
} );

}

Thanks
0
Matt
Top achievements
Rank 1
answered on 02 Dec 2011, 04:20 PM
Pablo -

Adding such code doesn't work, either.  During the post back, when ItemCreated is called, item.FindControl( "phDate" ) does equal null.  If I set a breakpoint, I can check and the cell is empty.

Two points to note about this solution that you proposed. 
  • First, it currently works with everything except the RadDatePicker.  I have scenarios where I'm using a standard TextBox, and I tried the RadTextBox the other day.  In both cases, I always add a new item during ItemCreated, and the value is properly populated when I get in to ItemCommand.
  • Second, when I examine the control that is found during ItemCommand, the item.FindControl("phDate").DateInput.SelectedDate is non-null and properly populated.  This would indicate that something is being properly passed back, just not FindControl("phDate").SelectedDate.  While I could make this work for reading the data during ItemCommand, it doesn't help return the value to the grid after the postback is complete.  Without going through the entire grid again, all of the date controls would continue to show as empty.

I appreciate the help.  I'll try to be more responsive with any future ideas - apparently "Subscribe" does not mean email when there are new posts, so I'll have to manually check in during the day.

Thanks,
Matt

0
Mira
Telerik team
answered on 06 Dec 2011, 09:59 AM
Hello Matt,

I have followed your scenario and prepared a sample project for you demonstrating how the desired functionality can be implemented. You can find it attached to this message.

I hope it helps.

Best wishes,
Mira
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Matt
Top achievements
Rank 1
Answers by
Pablo
Top achievements
Rank 1
Matt
Top achievements
Rank 1
Mira
Telerik team
Share this question
or