Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
50
How to get or set the value of a webdatetimeedit control with JavaScript
posted

I have an aspx page with a webdatetimeedit control on it. How do I get or set the value from JavaScript?

I need to do this:

  • get the initial value (from the server) when the page loads,
  • save it to a variable,
  • let the user edit the date in the control,
  • then restore the initial value back into the control when the user hits a "Abandon Edits" button.
  • I need to do this with JavaScript because postbacks are so slow on this website.

It works fine with my textboxes and my dropdowns.

Here's my JavaScript code:

<script language="javascript" type="text/javascript">
    var strInitialWebdatetimeeditValue = "";
    var strInitialTextboxValue = "";
   
    function SaveInitialValues()
    {
        strInitialMyDateTimeEditValue = document.getElementById('<%=MyDateTimeEdit.ClientID%>').text;
        strInitialMyTextboxValue = document.getElementById('<%=MyTextbox.ClientID%>').value;
    }
   
    function RestoreInitialValues()
    {
        document.getElementById('<%=MyDateTimeEdit.ClientID%>').value = strInitialMyDateTimeEditValue;
        document.getElementById('<%=MyTextbox.ClientID%>').value = strInitialMyTextboxValue;
    }
</script>

<tr>
 <td>
   <igtxt:webdatetimeedit id="MyDateTimeEdit" runat="server" bordercolor="#000050"
    borderstyle="Solid" borderwidth="1px" usebrowserdefaults="False">
  <ButtonsAppearance>
   <ButtonStyle BackColor="Control" BorderColor="Control" BorderStyle="Solid"
    BorderWidth="1px">
   </ButtonStyle>
   <ButtonHoverStyle BackColor="#B8C0E0" BorderColor="#FFFFFF">
   </ButtonHoverStyle>
   <ButtonPressedStyle BackColor="#8090B0" BorderColor="#FFFFFF">
   </ButtonPressedStyle>
  </ButtonsAppearance>
   </igtxt:WebDateTimeEdit>
 </td>
</tr>

<tr>
 <td>
  <asp:TextBox ID="MyTextbox" TabIndex="3" runat="server" Width="244"></asp:TextBox>
 </td>
</tr> 

  • 45049
    Verified Answer
    posted

    You need to get a reference to the WebDateTimeEdit control through its client-side object model (CSOM), rather than as an HTML element.  This exposes the methods that can be used to get or set the text of th control (or to get its value as a date).

    Here are modified versions of the two functions you've listed:

        function SaveInitialValues()
        {
            strInitialMyDateTimeEditValue = igedit_getById('<%=MyDateTimeEdit.ClientID%>').getText();
            strInitialMyTextboxValue = document.getElementById('<%=MyTextbox.ClientID%>').value;
        }
       
        function RestoreInitialValues()
        {
            igedit_getById('<%=MyDateTimeEdit.ClientID%>').setText(strInitialMyDateTimeEditValue);
            document.getElementById('<%=MyTextbox.ClientID%>').value = strInitialMyTextboxValue;
        }

    You can get more information on the CSOM of all WebEditors from the WebEditors CSOM Overview (as exists in our online help documentation of NetAdvantage for Web Client 2009 Volume 1).  This article links to documentation specific to individual WebEditor controls, including WebDateTimeEdit.