簡體   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