1

我想在输入字符时更改与文本框相同的标签值。所以这里有一个问题?我如何在回发后维护文本框光标?例如,我输入“ab”。文本框光标位置将保持在最后一个字符'b'

这是我的编码

<script language="javascript" type="text/javascript">


 function RefreshUpdatePanel() {
     __doPostBack('<%= TextBox1.ClientID %>', '');
 };


</script>
<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
<ContentTemplate>

    <br />
    <br />
<asp:TextBox ID="TextBox1" runat="server" onkeyup="RefreshUpdatePanel();" 
            ontextchanged="TextBox1_TextChanged"></asp:TextBox>

     <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
         <ContentTemplate>
             <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
         </ContentTemplate>
    <Triggers>
    <asp:AsyncPostBackTrigger ControlID="TextBox1" />
    </Triggers>
    </asp:UpdatePanel>

    </ContentTemplate>
</asp:UpdatePanel>

我的后端代码

 protected void Page_Load(object sender, EventArgs e)
    {
        //--If post back is txtSearach , then do search function--
        if (Page.Request.Form["__EVENTTARGET"] == TextBox1.ClientID)
        {
            this.Label1.Text = this.TextBox1.Text;

        }

    }

我也尝试在回发事件中放置 textbox1.focus() ,但文本框光标位置将从第一个字符开始:(

4

2 回答 2

0

您需要做的是处理onsubmit表单(或文档)的事件,然后将当前插入符号位置存储在隐藏字段中。在服务器上,读取隐藏字段的值,然后生成启动脚本来设置插入符号的位置。棘手的位正在获取和设置插入符号位置 - 以下链接将在这方面为您提供帮助:

http://parentnode.org/javascript/working-with-the-cursor-position/
jQuery 在文本区域中
设置光标位置 在 html 文本框中设置键盘插入符号位置

说了这么多,我会建议你重新构建你的解决方案。您应该使用脚本服务(或页面方法)对服务器进行 ajax 调用以获取搜索结果,而不是对文本更改进行更新面板部分回发。它会更简单、优雅和高效。以下链接应该可以帮助您入门:

http://encosia.com/using-jquery-to-consume-aspnet-json-web-services/
http://www.codeproject.com/KB/aspnet/jQuery_To_WCF.aspx
http://msdn.microsoft.com/en-us/magazine/cc163499.aspx

于 2011-12-15T05:14:38.667 回答
-1

我强烈建议您查看一些可用的 jquery 插件。jquery autocomplete的快速谷歌搜索将返回相当多的选项。这是一个可以帮助您入门的方法。

http://jqueryui.com/demos/autocomplete/

于 2011-12-15T05:13:23.440 回答