Abstract: This article demonstrates how to autoscroll a multiline textbox both upwards and downwards using jQuery 1.3.2
AutoScroll an ASP.NET Multiline TextBox using jQuery
This article demonstrates how to autoscroll a multiline textbox both upwards and downwards using jQuery 1.3.2. This article is a sample chapter from my EBook called 51 Tips, Tricks and Recipes with jQuery and ASP.NET Controls. The chapter has been modified a little to publish it as an article.
Note that for demonstration purposes, I have included jQuery code in the same page. Ideally, these resources should be created in separate folders for maintainability. The code shown below has been tested on IE7, 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>
When the user clicks on the button (btnScroll), we toggle the click behavior. 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);
When the user clicks the button (btnScroll) again, the postback is cancelled and the scrollHeight is set to 0 (zero). This is done for scrolling upwards.
e.preventDefault();
scrollArea($tb, 0);
The scrollArea() function accepts the textarea that is to be scrolled as well as the scrollHeight. We then animate the scrollTop property to scroll upwards/downwards depending on the height parameter. The duration of the animation is set to 1000 milliseconds which provides a smooth 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.
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:
Post a Comment