1

I have a web control in which I have a multiple line text box. I'm updating the contents of the text box using a timer_tick event. I want to display the last line of the text box every time the timer_tick event is fired. Can some one help me?

Zach Green
  • 3,421
  • 4
  • 29
  • 32
krrish
  • 163
  • 5
  • 12

2 Answers2

0

I think the OP was looking for scrolling the text box to the bottom on every update. I found this which helped me.

Textbox1.SelectionStart = Textbox1.Text.Length - 1  
Textbox1.ScrollToCaret()  
Philip Allgaier
  • 3,505
  • 2
  • 26
  • 53
EquiGym
  • 21
  • 1
  • 1
  • 3
0

Your question is a bit unclear, but to get the last line of an ASP.NET TextBox on serverside:

Dim lastLine = txt.Text.Split({Environment.NewLine},
                            StringSplitOptions.None).Last

On clientside you could get the last line with this javascript:

var lines    = input.value.split('\n');
var lastLine = lines[lines.length-1];

Your question instead seem to be "how to scroll asp.net textbox to bottom?"

Handle Sys.WebForms.PageRequestManager.endRequest event and scroll the textbox down:

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

function EndRequestHandler(sender, args)
{
   if (args.get_error() == undefined)
   {
       var tbox = $get('<%= TextBox1.ClientID %>');
       tbox.scrollTop = tbox.scrollHeight;
   }
}
Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • for the chat application I'm creating, As you know I'm using a timer on the text box to pull out messages from the database and to see if a particular user have any messages. I'm writing code in the Timer_Tick event to check the database for every 2 seconds. When this timer is fired the Text box is scrolled to the top automatically for every two seconds. I want to show the bottom line of the text box where I'm displaying a new message to the user. – krrish Feb 08 '12 at 18:20
  • @krisch: So your question is instead: How to scroll to bottom of a multiline TextBox? – Tim Schmelter Feb 08 '12 at 19:00
  • yeah, you are correct(should display bottom every time it refreshes) – krrish Feb 08 '12 at 19:03
  • I used a similar setIntervel and setTimeout functions on the .ascx page but they are not working... Can you explain me a bit more. – krrish Feb 08 '12 at 19:14
  • In the web user control I won't have form tag.. probably is that causing error? – krrish Feb 08 '12 at 19:16
  • @krish: see my updated answer and the link to the `Sys.WebForms.PageRequestManager endRequest Event`. The form is not required since the UserControl will be rendered into the form of it's aspx-page. Notice that this is not codebehind but javascript. – Tim Schmelter Feb 08 '12 at 19:30