It is possible to add 'None' option in aja

2019-06-25 17:21发布

问题:

I have an <ajaxToolkit:CalendarExtender> object in my page. It puts the selected date on a disabled TextBox (populated at start with the today date). I would want a 'None' option in that calendar, for which the system would do a default operation (like it has the Today option). Is this possible? I searched information about this on the Internet, but I couldn't find anything relevant. I wonder if I missed something. Or do I have to implement a separate logic (like enabling the TextBox and the user could leave it blank as a 'None' selection)?

Are there any calendar objects, different from ajax toolkit calendar extender, that can be used with asp.net that offer a 'None' option?

I am still opened for an answer even thought it's just a 'No' (at least a little documented).

回答1:

There are two options available: the first one is to add some html element next to textbox and handle it click event in javascript. In this event handler clear calendar extender. This is a code:

 <script type="text/javascript">
      function clearDate(extenderId) {
           $find(extenderId).set_selectedDate(null);
      }
 </script>

<asp:TextBox runat="server" ID="Date1" autocomplete="off" />
<input type="button" value="x" onclick="clearDate('<%= defaultCalendarExtender.ClientID %>')" /> 
<br />
<ajaxToolkit:CalendarExtender ID="defaultCalendarExtender" runat="server" 
    TargetControlID="Date1" />

And the second solution - to tweak sources of the AjaxControlToolkit project. Actually, you need to change only the Client/MicrosoftAjax.Extended/Calendar/CalendarBehavior.pre.js file. Replace it with code below. There is a lot of code actually because I'm too lazy to explain each change step-by-step ;) In brief I had add new element to calendar popup footer to reset selected date to default selected value if it was specified or to null. You may tweak also the Calendar.css file next to CalendarBehavior.pre.js

Due to limitation on huge answers I had post javascript code on pastebin Pastebin link