【问题标题】:Appending text to textbox at the cursor location using APS.net C#使用 APS.net C# 将文本附加到光标位置的文本框
【发布时间】:2022-01-21 14:11:28
【问题描述】:

我正在尝试在 WebForm ASP.net 的文本框中插入一些文本。我的目标是当用户从下拉列表中选择项目时,该项目将被添加/插入到光标位置的文本框文本中。我尝试了不同的方式,但没有运气。我面临的问题是我无法找到光标位置。

我有文本框和下拉列表:

        <div class="row" style="text-align: right; align-content: baseline">
            <div class="col-sm-12 col-md-12 col-lg-12 margin_top_bottom_10px" style="display:flex">
                <asp:DropDownList ID="ddlElementToAdd" runat="server" Width="20%" />
            </div>
            <div class="col-sm-12 col-md-12 col-lg-12 margin_top_bottom_10px">
                <asp:TextBox ID="TextBox1" runat="server" Width="100%" Height="500" 
                TextMode="MultiLine"></asp:TextBox>                    
            </div>
        </div>

我将项目添加到加载页面的下拉列表中。

任何建议 谢谢

【问题讨论】:

  • C#代码只在服务器上运行,鼠标在用户浏览器上运行。这需要在浏览器中运行的脚本来完成。
  • 您可以向 DropDownList 添加一个 onchange 事件并将文本附加到文本框吗?

标签: c# asp.net


【解决方案1】:

嗯,你可以这样做。但它会像这样工作:

假设我们有两个文本框。我们可以点击文本框 - 输入,光标移动。

然后您想节省输入,因此您现在转到组合框,然后选择一个值,它会将组合框插入到您的文本和最后一个光标位置。

这需要的是,当您单击文本框或四处移动时,我们需要同时获取单击事件(保存光标位置)或编辑(同样,保存光标位置)。

此标记有效 - 混乱和快速 + 肮脏 - 但它确实有效,并且适用于两个编辑文本框。

我拥有的两个字段当然会(应该)用 style=display:none 隐藏。

但是,这段代码确实有效:

      <h4>Inset Hotel</h4>
        <asp:DropDownList ID="DropDownList1" runat="server" Width="231px" 
            DataSourceID="SqlDataSource1" DataTextField="HotelName" DataValueField="ID"
            ClientIDMode="Static">
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyCalendar.My.MySettings.TEST4 %>" SelectCommand="SELECT [ID], [HotelName] FROM [tblHotels] ORDER BY [HotelName]"></asp:SqlDataSource>
        <br />
        <br />
        <div style="float:left">
         <asp:TextBox ID="TextBox1" runat="server" Width="400px" TextMode="MultiLine"
             Height="240px"
             ClientIDMode="Static"
             ></asp:TextBox>
        </div>
        <div style="float:left;margin-left:25px">
            <asp:TextBox ID="TextBox2" runat="server" Width="400px" TextMode="MultiLine" 
                Height="240px"
                ClientIDMode="Static"
                ></asp:TextBox>
        </div>
        <br />
        <asp:TextBox ID="txtCursor" runat="server" ClientIDMode="Static"></asp:TextBox>
        <asp:TextBox ID="txtControl" runat="server" ClientIDMode="Static"></asp:TextBox>
        <asp:TextBox ID="TextBox5" runat="server" ClientIDMode="Static"></asp:TextBox>

    </div>

    <script>
        $(document).ready(function () {
            //makes 'DetectChange' the handler when TextBox1 changes...
            $('#TextBox1,#TextBox2').click(function () {
                DetectChange(this);
            });
            $('#TextBox1,#TextBox2').keyup(function () {
                DetectChange(this);
            });

            $('#DropDownList1').change(function () {
                c = $(this)
                var SelectedValue = c.val();
                if (SelectedValue > 0) {
                    //get selected text and set to label
                    var SelectedText = $("#DropDownList1 option:selected").text();
                    console.log(SelectedText)
                    $('#TextBox5').val(SelectedText);

                    tBox = $('#txtControl').val()
                    tCursor = $('#txtCursor').val()
                    tEditBox = $('#' + tBox)
                    newResult = tEditBox.val()

                    if (tCursor >= 0) {
                        newResult = newResult.substring(0, tCursor) + SelectedText + newResult.substring(tCursor)
                        tEditBox.val(newResult)
                    }
                }
            });
        });

        function DetectChange(MyT) {
            c = $(MyT)
            $("#txtCursor").val(c.prop("selectionStart"))
            $("#txtControl").val(c.attr("ID"))
        }

    </script>

所以它看起来像这样:

因此,您现在可以单击或键入 + 编辑任一文本框,如果您转到组合框,请选择一个选项 - 文本将插入到您当前的位置。它适用于任一文本框。

【讨论】:

    【解决方案2】:

    当您选择下拉菜单时,焦点位于下拉菜单上。它现在不在文本框中,因此您无法获取文本框的光标位置,因为技术上没有。如果您愿意,可以在 texbox 的末尾或使用下拉 onchange 事件在特定位置添加文本

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 2016-09-07
    • 2020-10-24
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多