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

Setting MinDate/MaxDate in EditItemTemplate of RadGrid

8 Answers 405 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
Jon
Top achievements
Rank 1
Jon asked on 31 Jan 2011, 09:58 PM
I am having trouble setting the default values I want to use for MinDate and MaxDate within the EditItemTemplate of a GridTemplateColumn within a RadGrid. Since this control is nested so deeply, I don't have direct access to it in code-behind, so I wanted to set the MinDate and MaxDate in-line in the page (.aspx) markup.

Here's what I have currently for the GridTemplateColumn (I have omitted the MaxDate for the time being, which would be set to the current date, i.e. DateTime.Today):

<telerik:GridTemplateColumn DataField="ScheduleDate" HeaderButtonType="None" 
    HeaderText="Day" SortExpression="ScheduleDate" UniqueName="ScheduleDate">
    <EditItemTemplate>
        <asp:Label ID="Label_ScheduleDate" runat="server" AssociatedControlID = "RadDatePicker_ScheduleDate"></asp:Label>
        <telerik:RadDatePicker ID="RadDatePicker_ScheduleDate" UniqueName="RadDatePicker_ScheduleDate" runat="server" Width="80px" 
            Skin="Outlook" MinDate='<%= System.DateTime.Today().AddDays(-30) %>'>
            <DateInput onclick="$find('<%= RadDatePicker_ScheduleDate.ClientID %>').showPopup();">
            </DateInput>
            <Calendar DayNameFormat="FirstTwoLetters" FirstDayOfWeek="Default">
            </Calendar>
            <DatePopupButton Visible="true" />
        </telerik:RadDatePicker>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="ScheduleDateLabel" runat="server" 
            Text='<%# Eval("ScheduleDate", "{0:d}") %>'></asp:Label>
    </ItemTemplate>
</telerik:GridTemplateColumn>

In the Visual Studio 2008 designer, I get an error from the parser:

"Error Creating Control - RadGrid_StaffBreakout: Cannot create an object of type 'System.DateTime' from its string representation '<%= System.DateTime.Today().AddDays(-30) %>' for the 'MinDate' property."

According to the MSDN documentation, this returns a DateTime structure (not an object, since DateTime is a value type). I tried several work arounds like using ToString() and also combining ToString with DateTime.Parse, but none of these worked.

How can I set the MinDate and MaxDate values in the mark-up of the page?

I am using "RAD Controls for ASP.NET AJAX Q2 2010 - SP2"

Thanks,

Jon

8 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 03 Feb 2011, 08:13 AM
Hello Jon,

You can access the DatePicker from codebehind and set the min date like below.
C#:
protected void rad_ItemDataBound(object sender, GridItemEventArgs e)
    
if (e.Item is GridEditFormItem && e.Item.IsInEditMode)//popup&editform
        {
            GridEditFormItem edititem = (GridEditFormItem)e.Item;
            RadDatePicker txt1 = (RadDatePicker)edititem.FindControl("RadDatePicker_ScheduleDate");
            txt1.MinDate = System.DateTime.Now.AddDays(-30);
         }
     }

Thanks,

Shinu.
0
Jon
Top achievements
Rank 1
answered on 09 Feb 2011, 08:07 PM

Shinu,

Thank you for your suggestion. I ended up using the server-side idea, but still think and wish this could be done client-side (which to me seems to be a bug). IMHO, the syntax I used was standard aspx markup, so I'm puzzled why the RadGrid is not honoring this client-side.

Thanks again,

Jon

0
Radoslav
Telerik team
answered on 14 Feb 2011, 12:46 PM
Hello Jon,

Could you please try using the following code snippet and let me know if the issue still exists:
<telerik:GridTemplateColumn DataField="ScheduleDate" HeaderButtonType="None"
    HeaderText="Day" SortExpression="ScheduleDate" UniqueName="ScheduleDate">
    <EditItemTemplate>
        <asp:Label ID="Label_ScheduleDate" runat="server" AssociatedControlID = "RadDatePicker_ScheduleDate"></asp:Label>
        <telerik:RadDatePicker ID="RadDatePicker_ScheduleDate" UniqueName="RadDatePicker_ScheduleDate" runat="server" Width="80px"
            Skin="Outlook" MinDate='<%# System.DateTime.Today().AddDays(-30) %>'>
            <DateInput onclick="$find('<%= RadDatePicker_ScheduleDate.ClientID %>').showPopup();">
            </DateInput>
            <Calendar DayNameFormat="FirstTwoLetters" FirstDayOfWeek="Default">
            </Calendar>
            <DatePopupButton Visible="true" />
        </telerik:RadDatePicker>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="ScheduleDateLabel" runat="server"
            Text='<%# Eval("ScheduleDate", "{0:d}") %>'></asp:Label>
    </ItemTemplate>
</telerik:GridTemplateColumn>

Additionally I am sending you a simple example which demonstrates the desired functionality.

I hope this helps.

Greetings,
Radoslav
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.
0
Bill
Top achievements
Rank 1
answered on 18 Aug 2011, 07:09 PM
I tried the way Radoslav suggested and it worked for me.
0
Mark Mazzitello
Top achievements
Rank 1
answered on 02 Nov 2011, 05:00 PM
Thank you - I searched a long time to find out how to set the minDate to the current date from client side code.  
This example from Radoslav works perfectly, except I was getting a compile error and I had to remove the () after the "Today" so it reads:  
MinDate='<%# System.DateTime.Today.AddDays(-30) %>'
0
Kiran
Top achievements
Rank 1
answered on 06 Aug 2013, 07:11 PM
Hello.. can anyone please explain why this is not working with the code below. With the below code, the RadDateTimePicker still allows me to select the previous dates.  I could not attach the sample project here but please let me know if needed so that i can attach the same. Attached the screenshot of the sample page though, having the below controls.  And the version of Telerik is 2012.2.815.40

<Telerik:RadDateTimePicker runat="server" ID="dtNeedBy" MinDate="<%# DateTime.Today %>" />
<Telerik:RadDateTimePicker runat="server" ID="RadDateTimePicker1" MinDate="<%# DateTime.Today.AddDays(-30) %>" />
0
Shinu
Top achievements
Rank 2
answered on 07 Aug 2013, 09:40 AM
Hi Kiran,

Please try invoking the Page.DataBind() in Page_Load event which worked as expected.
 
ASPX:
<telerik:RadDateTimePicker runat="server" ID="dtNeedBy" MinDate="<%# DateTime.Today %>" />
<telerik:RadDateTimePicker runat="server" ID="RadDateTimePicker1" MinDate="<%# DateTime.Today.AddDays(-30) %>" />

C#:
protected void Page_Load(object sender, EventArgs e)
{
    Page.DataBind();
}

Thanks,
Shinu.
0
Kiran
Top achievements
Rank 1
answered on 08 Aug 2013, 09:06 PM
Thank you very much Shinu. It worked.
Tags
Calendar
Asked by
Jon
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Jon
Top achievements
Rank 1
Radoslav
Telerik team
Bill
Top achievements
Rank 1
Mark Mazzitello
Top achievements
Rank 1
Kiran
Top achievements
Rank 1
Share this question
or