Thursday, February 11, 2010

AutoScroll an ASP.NET Multiline TextBox using jQuery

Thisarticle demonstrates how to autoscroll a multiline textbox both upwardsand downwards using jQuery 1.3.2. This article is a sample chapter frommy EBook called 51 Tips, Tricks and Recipes with jQumry and ASP.NET Controls. The chapter has been modified a little to publish it as an article.


Notethat for demonstration purposes, I have included jQuery code in thesame page. Ideally, these resources should be created in separatefolders for maintainability. The code shown below has been tested onIE7, Firefox 3, Chrome 2 and Safari 4


Let us quickly jump to the solution and see how to AutoScroll a multiline textbox
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>AutoScroll a Multiline TextBox</title>
    <script type="text/javascript"
     src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
    </script>


    <script type="text/javascript">
        $(function() {
            var $tb = $('textarea[id$=tb1]');
            $('input[id$=btnScroll]').toggle(
            function(e) {
                e.preventDefault();
                scrollArea($tb, $tb[0].scrollHeight);
            },


            function(e) {
                e.preventDefault();
                scrollArea($tb, 0);
            });
        });


        function scrollArea(ctrl, ht) {
            ctrl.animate({ scrollTop: ht }, 1000);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div class="smallDiv">
        <h2>Scroll the box contents by clicking on the Button</h2>
        <br /><br />
        <asp:TextBox ID="tb1" runat="server" TextMode="MultiLine"
        Text="Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem
        Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum
        Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum
        Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum
        Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum"
        Rows="5"/>
        <br />
        <asp:Button ID="btnScroll" runat="server" Text="Scroll"/>
        <br /><br />
        Tip: The Text can be scrolled both downwards and upwards.
    </div>
    </form>
</body>
</html>
Whenthe user clicks on the button (btnScroll), we toggle the clickbehavior. On the first click, we cancel the postback by using e.preventDefault() and then call a function called scrollArea() passing in the textarea and the scrollHeight. The code $tb[0].scrollHeight is for scrolling downwards
e.preventDefault();
scrollArea($tb, $tb[0].scrollHeight);
Whenthe user clicks the button (btnScroll) again, the postback is cancelledand the scrollHeight is set to 0 (zero). This is done for scrollingupwards.
e.preventDefault();
scrollArea($tb, 0);
ThescrollArea() function accepts the textarea that is to be scrolled aswell as the scrollHeight. We then animate the scrollTop property toscroll upwards/downwards depending on the height parameter. Theduration of the animation is set to 1000 milliseconds which provides asmooth scrolling effect
function scrollArea(ctrl, ht) {
    ctrl.animate({ scrollTop: ht }, 1000);
}
The code here assumes that you are not scrolling the textarea manually and then clicking the Scroll button.
You can see a Live Demo over here.


I hope you found this article useful and I thank you for viewing it. This article was taken from my EBook called 51 Tips, Tricks and Recipes with jQuery and ASP.NET Controls which contains similar recipes that you can use in your applications.

No comments: