简体   繁体   中英

How can I set the cursor position at the new line which is after the last line in a multiline textbox using C#?

I have created a chat application, and using using multiline textbox to show the chat messages.

**Aspx page :**

<asp:UpdatePanel runat="server" ID="up1" UpdateMode="Conditional">
<ContentTemplate>
 <asp:TextBox ID="TextBox1" runat="server" Height="163px" Font-Size="0.81em" 
  Font-Names="Verdana" TextMode="MultiLine"></asp:TextBox> 
</ContentTemplate>
 <Triggers>
   <asp:AsyncPostBackTrigger ControlID="refreshTimer" EventName="Tick" />
 </Triggers>
 </asp:UpdatePanel>  

 <asp:Timer runat="server" ID="refreshTimer" Interval="2000" Enabled="true" 
 ontick="refreshTimer_Tick" />

 **Behind code :**

 protected void refreshTimer_Tick(object sender, EventArgs e)
 {
     up1.Update();
 }

Everything is working fine but after having more messages the TextBox1 refreshes and the last lines get hidden with vertical scrollbar shown, how can I have cursor position at the new line which is after the last line.

Thanks in advance....... :)

I think you can set ClientIDMode="Static" for the textbox as follow:

<asp:TextBox ID="TextBox1" runat="server" Height="163px" ClientIDMode="Static" Font-Size="0.81em" Font-Names="Verdana" TextMode="MultiLine"></asp:TextBox> 

and after that to put the folling javascript on the page:

<script type="text/javascript">
    $(document).ready(function () {
        $('#TextBox1').live('change', function () {
            $('#TextBox1').scrollTop($('#TextBox1')[0].scrollHeight - $('#TextBox1').height());
        });
    });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM