how to show an arrow button when scrolling a gridview in panel using java script and asp.net?

HOUSSEM MAHJOUBI 286 Reputation points
2023-03-17T08:30:40.42+00:00

Hi members

i want to show a button when scrolling a gridview in panel

i have tried in normal page and thats work fine this is my code

<script type="text/javascript">  
    $(document).ready(function () {  
        //Hide Back to top button  
        $("#btnScrollToTop").hide();  
        $(window).scroll(function () {  
            if ($(window).scrollTop() > 40) {  
                $('#btnScrollToTop').fadeIn();  
            } else {  
                $('#btnScrollToTop').fadeOut();  
            }  
        });  
        $('#btnScrollToTop').click(function () {  
            $('body').animate({  
                scrollTop: 0  
            }, 1000);  
        });  
    });  
</script> 

how i can do that ?

please help

Microsoft 365 and Office Development Office JavaScript API
Developer technologies VB
Developer technologies ASP.NET Other
{count} votes

Accepted answer
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2023-03-20T05:53:39.25+00:00

    Hi @HOUSSEM MAHJOUBI ,

    Your code is setting the window's scrollbar, so it doesn't work.

    What you need to set is the scrollbar of the panel.$("#pnlScroll").scroll

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <title></title>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {          
                $("#btnScrollToTop").hide();
                $(function () {
                    $("#pnlScroll").scroll(function () {
                        if ($(this).scrollTop() > 10) {
                            $('#btnScrollToTop').fadeIn();
                        } else {
                            $('#btnScrollToTop').fadeOut();
                        }
                    });
                    $('#btnScrollToTop').click(function () {
                        $('#pnlScroll').animate({
                            scrollTop: 0
                        }, 800);
                        return false;
                    });
                });
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">                                  
        <asp:Panel ID="pnlScroll" ScrollBars="Vertical" runat="server" Height="400px"
            Width="300px">        
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"></asp:GridView>             
               <asp:Button ID="btnScrollToTop" CssClass="button" Text="Scroll up" runat="server" />
        </asp:Panel>                                                             
        </form>
    </body>
    </html>
    

    8

    Best regards,

    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. peiye zhu 165 Reputation points
    2023-03-20T09:15:03.41+00:00
    
    function debounce(fn, wait) {
        var timeout = null;
        return function() {
            if(timeout !== null) clearTimeout(timeout);
            timeout = setTimeout(fn, wait);
        }
    }
    function handle() {
    	if(panel.scrollTop>100){
    btnScrollToTop.classList.remove("hide");	
    	}else{
    btnScrollToTop.classList.add("hide");	
    	}
    }
    panel.addEventListener('scroll', debounce(handle, 500));	
    		btnScrollToTop.onclick=function(){
    panel.scrollTop=0
    		}
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.