|
| 1 | +--- |
| 2 | +title: Preventing RadDropDownList from Opening on Scroll in Mobile Devices |
| 3 | +description: This article demonstrates how to prevent the RadDropDownList from unintentionally opening when scrolling on a page on mobile devices. |
| 4 | +type: how-to |
| 5 | +page_title: Prevent DropDown From Opening while Scrolling on Mobile Devices |
| 6 | +slug: dropdownlist-prevent-dropdown-opening-while-scrolling-on-mobile-devices |
| 7 | +tags: javascript, touch events, mobile, scroll, raddropdownlist, asp.net ajax |
| 8 | +res_type: kb |
| 9 | +ticketid: 1672206 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | +<tbody> |
| 16 | +<tr> |
| 17 | +<td>Product</td> |
| 18 | +<td>RadDropDownList for ASP.NET AJAX</td> |
| 19 | +</tr> |
| 20 | +<tr> |
| 21 | +<td>Version</td> |
| 22 | +<td>All</td> |
| 23 | +</tr> |
| 24 | +</tbody> |
| 25 | +</table> |
| 26 | + |
| 27 | +## Description |
| 28 | + |
| 29 | +When interacting with a web page on an Android mobile device, scrolling over a RadDropDownList unintentionally opens it. This behavior contrasts with that of RadComboBox, which does not exhibit the same issue. The goal is to modify the RadDropDownList such that it only opens upon an explicit tap, not during scrolling. |
| 30 | + |
| 31 | +This knowledge-base article also answers the following questions: |
| 32 | + |
| 33 | +- How can I stop RadDropDownList from opening when scrolling on mobile? |
| 34 | +- What JavaScript can prevent RadDropDownList from triggering on touch scroll? |
| 35 | +- Is there a way to make RadDropDownList behave like RadComboBox on mobile devices? |
| 36 | + |
| 37 | +## Solution |
| 38 | + |
| 39 | +To prevent the RadDropDownList from opening during a scroll on mobile devices, implement custom JavaScript to handle touch events, specifically detecting scroll gestures and inhibiting the dropdown from opening during such gestures. |
| 40 | + |
| 41 | +### Custom JavaScript |
| 42 | + |
| 43 | +Incorporate the following script to manage touch events and prevent the dropdown from opening while scrolling: |
| 44 | + |
| 45 | +````JavaScript |
| 46 | +let isScrolling = false; |
| 47 | + |
| 48 | +document.addEventListener('DOMContentLoaded', function () { |
| 49 | + let dropdowns = document.querySelectorAll('.RadDropDownList'); |
| 50 | + |
| 51 | + dropdowns.forEach(function (dropdown) { |
| 52 | + dropdown.addEventListener('touchstart', function (e) { |
| 53 | + isScrolling = false; |
| 54 | + }); |
| 55 | + |
| 56 | + dropdown.addEventListener('touchmove', function (e) { |
| 57 | + isScrolling = true; |
| 58 | + }); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +function onClientDropDownOpening(sender, args) { |
| 63 | + if (isScrolling) { |
| 64 | + args.set_cancel(true); |
| 65 | + } |
| 66 | +} |
| 67 | +```` |
| 68 | + |
| 69 | +Ensure this script is included on your page, following the RadScriptManager to ensure it loads after the necessary Telerik UI scripts. |
| 70 | + |
| 71 | +### Explanation |
| 72 | + |
| 73 | +- **Touch Events**: The script listens for `touchstart` and `touchmove` events on RadDropDownList elements. |
| 74 | +- **Scroll Detection**: It toggles a flag (`isScrolling`) upon detecting a touch movement (`touchmove`). |
| 75 | +- **DropDownOpening**: It leverages the `onClientDropDownOpening` event, canceling it if scrolling is detected (`isScrolling` is true). |
| 76 | + |
| 77 | +### Additional Considerations |
| 78 | + |
| 79 | +- **Testing**: Verify the solution across different mobile devices and browsers for comprehensive compatibility. |
| 80 | +- **Performance**: Pay attention to performance implications, especially with multiple dropdowns on your page. |
| 81 | + |
| 82 | +Implementing this solution aligns the behavior of the RadDropDownList with the RadComboBox, ensuring a seamless and user-friendly experience on mobile devices. |
| 83 | + |
| 84 | +## See Also |
| 85 | + |
| 86 | +- [Mobile Browser Support in Telerik UI for ASP.NET AJAX](https://www.telerik.com/forums/mobile-browser) |
| 87 | +- [RadDropDownList Documentation](https://docs.telerik.com/devtools/aspnet-ajax/controls/dropdownlist/dropdownlist-vs-radcombobox) |
| 88 | +- [Handling Touch Events in JavaScript](https://developer.mozilla.org/en-US/docs/Web/API/Touch_events) |
0 commit comments