?
Solved

How to render a multiline text box so that it displays the last line of text ?

Posted on 2008-11-16
5
Medium Priority
?
603 Views
Last Modified: 2013-12-17
I am a rookie in asp.net , C# and using Visual Studio 2008 . In my program I am using a multi-line text box to display a chat conversation . Its working fine. But when the text in it overruns the size of the textbox set at design time , the textbox is rendered (on click of a button) such that it displays only the first few lines of textbox . And i have to scroll down to see the latest line in the conversation. Is there any way that when i press the button(send) the textbox opens such that it is displays the last lines of the text and i dont have to scroll down ?

Is it possible or would i have to make a custom control?
0
Comment
Question by:Saurabh Singh Teotia
  • 3
  • 2
5 Comments
 
LVL 8

Expert Comment

by:tiagosalgado
ID: 22971961
You can use Javascript to do that.
<script type="text/javascript">
function scrollTextBoxDown(elementId)
{
 var textRange = document.getElementById(elementId).createTextRange();
 textRange.collapse(false);
 textRange.select();
}
</script>

Open in new window

0
 
LVL 59

Author Comment

by:Saurabh Singh Teotia
ID: 22973883
Pardon my ignorance but where do i put this code in the .cs file or the html source file ? I am  using a button(Button1) and a textbox(textbox 1) with its type set to multiline  . So should i put this in the Button1_Click function in the .cs file. Besides how do I identify the Textbox in this code : by the element id ?

Also i think my earlier question was a bit unclear . I need to display a chat conversatio n in a multiline textbox. So whenver the textbox is rendered/refreshed i want the cursor to be at the last line of text box , so that i dont have to scroll down to see the latest message.

 
0
 
LVL 8

Expert Comment

by:tiagosalgado
ID: 22975555
In your asp.net page add this JS function

<script type="text/javascript">
        function scrollTextBoxDown(elementId) {
            var textRange = elementId.createTextRange();
            textRange.collapse(false);
            textRange.select();
        }
</script>

On your <form> tag, add the "defaultfocus" property and set it to your textbox id
<form defaultfocus="Textbox_Where_Your_Chat_Conversation_Is" id="form1" runat="server">

On code-behing, add this line to your page_load
Textbox_Where_Your_Chat_Conversation_Is.Attributes.Add("onfocus", "scrollTextBoxDown(" + Textbox_Where_Your_Chat_Conversation_Is.UniqueID + ");");

0
 
LVL 59

Author Comment

by:Saurabh Singh Teotia
ID: 22978839
sorry but it didnt work . i am uploading the files here so perhaps you can pinpoint where the fault lies. I am using the ajax controls provided by the visual studio 2008 .
thanks .
source-and-cs.doc
0
 
LVL 8

Accepted Solution

by:
tiagosalgado earned 2000 total points
ID: 22980390
I've change your <form> tag to <form defaultfocus="TextBox1" id="form1" runat="server">

And i add TextBox1.Attributes.Add("onfocus", "scrollTextBoxDown(" + TextBox1.UniqueID + ");"); to your Page_Load and it works well.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question