1

i want to change the label value same as textbox while i entering a character. so here got a question?How can i maintain the textbox cursor after the postback ?For example i type 'ab'. The textbox cursor position will remain at the last character which is 'b'

Here is my coding

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

my back end code

 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;

        }

    }

I also try put textbox1.focus() while the postback event, but the textbox cursor posittion will start at the first character :(

user998405
  • 1,329
  • 5
  • 41
  • 84
  • 1
    I'm curious why you would want to postback on textchanged. You could use javascript/jQuery to update the label without the round trip. – Cj S. Dec 15 '11 at 04:56
  • actually i want do to instant search function. When user key in the search textbox , then the gridview will update also. I dunno how to use Jquery to rebind my gridview :( – user998405 Dec 15 '11 at 05:01
  • I would spend a bit of time looking at jQuery. It'll be worth it. – Cj S. Dec 15 '11 at 05:08

2 Answers2

0

What you need to do is to handle onsubmit event of form (or document) and then store the current caret position in a hidden field. On the server, read the hidden field value and then generate the start-up script to set the caret position. Tricky bits are getting and setting caret position - below links will help you in that matter:

http://parentnode.org/javascript/working-with-the-cursor-position/
jQuery Set Cursor Position in Text Area
Set keyboard caret position in html textbox

Said all that, I will recommend you to re-structure your solution. Instead of taking update-panel partial post-back on text change, you should use script services (or page methods) to make the ajax call to server for getting search results. It would be more simple, elegant and efficient. Below links should get you started:

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

Community
  • 1
  • 1
VinayC
  • 47,395
  • 5
  • 59
  • 72
-1

I would highly recommend checking out some of the jquery plugins available. A quick google search of jquery autocomplete will return a pretty large amount of options. Here's one to get you started.

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

Robert
  • 3,074
  • 3
  • 24
  • 32