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 🙁
What you need to do is to handle
onsubmitevent 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