Setting MinDate/MaxDate in EditItemTemplate of RadGrid

9 posts, 0 answers
  1. Jon
    Jon avatar
    22 posts
    Member since:
    Oct 2010

    Posted 31 Jan 2011 Link to this post

    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
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 03 Feb 2011 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jon
    Jon avatar
    22 posts
    Member since:
    Oct 2010

    Posted 09 Feb 2011 Link to this post

    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

  5. Radoslav
    Admin
    Radoslav avatar
    1564 posts

    Posted 14 Feb 2011 Link to this post

    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.
  6. Bill
    Bill avatar
    1 posts
    Member since:
    Dec 2010

    Posted 18 Aug 2011 Link to this post

    I tried the way Radoslav suggested and it worked for me.
  7. Mark Mazzitello
    Mark Mazzitello avatar
    3 posts
    Member since:
    May 2010

    Posted 02 Nov 2011 Link to this post

    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) %>'
  8. Kiran
    Kiran avatar
    5 posts
    Member since:
    Oct 2012

    Posted 06 Aug 2013 Link to this post

    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) %>" />
  9. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 07 Aug 2013 Link to this post

    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.
  10. Kiran
    Kiran avatar
    5 posts
    Member since:
    Oct 2012

    Posted 08 Aug 2013 Link to this post

    Thank you very much Shinu. It worked.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017