locked
Does DateTimeControl even work? RRS feed

  • Question

  • Recently, I created a webpart on WSS 3.0 that requires a date picker.  The Ajax Control Tool kit is not available.  ASP.Net does not appear to have a built-in picker. I decided to use Sharepoint's DateTimeControl class.

    The problem is that it just doesn't work. Suppose I create a user control (which I then load) and do the following (sp is the prefix to the Sharepoint name space).

    <sp:DateTimeControl ID="DateTimeControl1" runat="server" />


    I then use the designer to modify the properites and then. In my code, I do something like:

    DateTimeControll1.SelectedDate = DateTime.Now;

    I run the webpart in WSS and got the following error:

    Multiple controls with the same ID

    DateTimeControl appears to be a wrapper class that uses an internal textbox and droplists. What seemed to have happened is that designer helpfully unwrap the control on my .ascx page. Unfortunately, the server also unwrap the DateTimeControl during runtime, resulting in two sets of textboxes and droplist. This is why we get the multiple ID error.

    I went back into the .ascx page and ripped out the unwrapped code. But when I run the code, I no longer get a confliect, but I do get the following error:

    [NullReferenceException: Object reference not set to an instance of an object.]
       Microsoft.SharePoint.WebControls.DateTimeControl.GetDateTimeFromTextBox() +143
       Microsoft.SharePoint.WebControls.DateTimeControl.ChangesByUser(Object sender, EventArgs e) +114
       System.Web.UI.WebControls.TextBox.OnTextChanged(EventArgs e) +1234972
       System.Web.UI.WebControls.TextBox.RaisePostDataChangedEvent() +52
       System.Web.UI.WebControls.TextBox.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() +7
       System.Web.UI.Page.RaiseChangedEvents() +117
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1646
     

    For some unknown reason, the DateTimeControl seems to lost access to its internal objects. I played around with it and can't get it to work. The only thing that worked is by setting the properties .DateOnly="true". This makes the two time drop down goes away and the DateTimeControl works normally. The problem is that I would like to have a date and time rather than the date.

    I have goggle and notice people with the same problem, but no one seems to have a solutions. Can someone explain some special tricks required to get DateTimeControl to work?

    I am fairly new to sharepoint and have run into corners when I use various class (for example,  SPDataSource does not appear to work properly either). Is there a list of classes that one should avoid or a list of eratas.

    Thanks

    Paul







     

    Tuesday, October 14, 2008 7:15 PM

Answers

  • The credit for figuring this out goes to Michael Vannata from Microsoft. We think it's a viewstate bug of some sort.

    Basically, if you turn off viewstate, the control will bomb with a null reference exception when you do a postback. You must make sure that the viewstate is true.

    If you add the control to a panel, things get stranger. The following will not work:

    <asp:panel id="mypanel" EnableViewState="false" runat="server" >
        <sp:DateTimeControl ID="MyDateTime" runat="server" EnableViewState="true"  />
    </asp:panel>

    Apparently, if you attach datetime control to a panel, viewstate must be turned on for the entire control tree path to the datetimecontrol.

    For example, the following is good because the top panel is set to true and the sub-panel and datetime control inherits EnableViewState="true" from the top panel.

    <asp:panel id="mypaneltop" EnableViewState="true" runat="server">
        <asp:panel id="mypanelmid" runat="server" >
            <sp:DateTimeControl ID="MyDateTime" runat="server" />
        </asp:panel>
    </asp:panel>

    The following will not work because the mid panel has enableviewstate="false"

    <asp:panel id="mypaneltop" EnableViewState="true" runat="server">
        <asp:panel id="mypanelmid" runat="server" EnableViewState="false" >
            <sp:DateTimeControl ID="MyDateTime" runat="server" EnableViewState="true" />
        </asp:panel>
    </asp:panel>


    I do not understand why this is happening, but this is what I observe so far. The workaround is to make sure that if you add the datetimecontrol to a panel, it is EnableViewState="true" from the top level control to the datetimecontrol.

    Mike Vannata has submitted this as a bug to Microsoft.

    • Marked as answer by Paul Siu Tuesday, November 11, 2008 8:45 PM
    • Edited by Paul Siu Tuesday, November 11, 2008 8:48 PM
    Tuesday, November 11, 2008 8:45 PM

All replies

  • Hi Paul,

    To help you on this, can you first show us how you use DateTimeControl in your web part?
    I am trying to reproduce your problem, but hard to do so without knowing how you code it.

    regards
    James

    James Tsai | MCPD | SharePoint Consultant
    My SharePoint Blog www.jamestsai.net/blog
    Wednesday, October 15, 2008 2:55 AM
  • I basically create user control with a plain HTML table. In one of the table cell, I put in a DateTimeControl, but that is not even necessary. I have basically do something like this in the CreateChildControl in the web part.

    DateTimeControl dtc = new DateTimeControl();
    Controls.Add(dtc);

    In addition to the DateTimeControl, there is an OK button where I attempt to read the SelectedDate. However, I keep getting the NullReference Exception when I call get on SelectedDate. The only time it work is if I set the DateOnly="true".

    I notice a recent post on this thread:

    http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/8081df85-d297-4423-98ad-84658051bb7d/

    The problem is similar to what the other poster describe. The value seems to died right after page load event. However, I turned off viewstate and set the SelectedDate value in the Page Load event and it still throws an exception.



    Wednesday, October 15, 2008 11:53 AM
  • The credit for figuring this out goes to Michael Vannata from Microsoft. We think it's a viewstate bug of some sort.

    Basically, if you turn off viewstate, the control will bomb with a null reference exception when you do a postback. You must make sure that the viewstate is true.

    If you add the control to a panel, things get stranger. The following will not work:

    <asp:panel id="mypanel" EnableViewState="false" runat="server" >
        <sp:DateTimeControl ID="MyDateTime" runat="server" EnableViewState="true"  />
    </asp:panel>

    Apparently, if you attach datetime control to a panel, viewstate must be turned on for the entire control tree path to the datetimecontrol.

    For example, the following is good because the top panel is set to true and the sub-panel and datetime control inherits EnableViewState="true" from the top panel.

    <asp:panel id="mypaneltop" EnableViewState="true" runat="server">
        <asp:panel id="mypanelmid" runat="server" >
            <sp:DateTimeControl ID="MyDateTime" runat="server" />
        </asp:panel>
    </asp:panel>

    The following will not work because the mid panel has enableviewstate="false"

    <asp:panel id="mypaneltop" EnableViewState="true" runat="server">
        <asp:panel id="mypanelmid" runat="server" EnableViewState="false" >
            <sp:DateTimeControl ID="MyDateTime" runat="server" EnableViewState="true" />
        </asp:panel>
    </asp:panel>


    I do not understand why this is happening, but this is what I observe so far. The workaround is to make sure that if you add the datetimecontrol to a panel, it is EnableViewState="true" from the top level control to the datetimecontrol.

    Mike Vannata has submitted this as a bug to Microsoft.

    • Marked as answer by Paul Siu Tuesday, November 11, 2008 8:45 PM
    • Edited by Paul Siu Tuesday, November 11, 2008 8:48 PM
    Tuesday, November 11, 2008 8:45 PM
  • I just ran into this as well, and can confirm that ensuring that EnableViewState is not set to false anywhere in the DateTimeControl's tree will resolve the issue.
    Tuesday, November 11, 2008 10:13 PM
  • Thank you! This saved my day! :)
    Tuesday, November 24, 2009 10:27 AM