繁体   English   中英

如何使用C#在多行文本框中最后一行之后的新行上设置光标位置?

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

我创建了一个聊天应用程序,并使用多行文本框显示聊天消息。

**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();
 }

一切工作正常,但是在收到更多消息后,TextBox1刷新并且最后一行被显示的垂直滚动条隐藏,如何将光标定位在最后一行之后的新行上。

提前致谢....... :)

我认为您可以为文本框设置ClientIDMode =“ Static”,如下所示:

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

然后在页面上放置以下javascript:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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